释放内存/跨平台兼容性的问题

时间:2012-07-27 18:49:50

标签: c gcc tree free runtime-error

我在C中创建了一个树,除了自由函数之外,当我使用Visual C ++时,一切都在我的计算机上运行。

当我在任何其他平台(DOS和UNIX上的gcc)上编译时,我在运行时也遇到很多问题。我不知道那里有什么不对。

在我的Visual C ++调试器上,它在(1)

处断开
void freePhoneBook(PhoneBookP P)
{
    traverseFree(P->Root);
}
static void traverseFree(NodeP N)
{
    if(N)
    {
        traverseFree(N->Left);
        traverseFree(N->Right);
        free(N);//(1)<------fails here
    }
}

错误说:

堆积在00A01768,修改为00A01798,要求大小为28 Windows在PhoneBook.exe中触发了断点。

这可能是由于堆损坏,这表示PhoneBook.exe或其加载的任何DLL中存在错误。

这也可能是由于用户在PhoneBook.exe具有焦点时按下F12。

输出窗口可能包含更多诊断信息。 HEAP [PhoneBook.exe]:为RtlValidateHeap指定的地址无效(00A00000,00A01770) Windows在PhoneBook.exe中触发了断点。

这可能是由于堆损坏,这表示PhoneBook.exe或其加载的任何DLL中存在错误。

这也可能是由于用户在PhoneBook.exe具有焦点时按下F12。

输出窗口可能包含更多诊断信息。

这是我的所有代码:

    /*
 * PhoneBook.h
 * Cop 3530 
 * jlewis
 */

#ifndef _phonebook_h
#define _phonebook_h

/*
 * PhoneBookP is a pointer to the phonebook struct
 * Define the phonebook struct and the node struct 
 * in your (.c) file
 */
typedef struct PhoneBookT *PhoneBookP;

/*
 * PhoneBook Interface
 */

/*
 * Returns a pointer to a new empty PhoneBook
 * If memory cannot be allocated, returns a NULL pointer
 */
PhoneBookP newPhoneBook();

/*
 * Locates and displays the desired entry from the phone book
 * If entry is not found, display an appropriate message
 * Parameters: book, firstname, lastname
 */
void lookupPhoneBook(PhoneBookP, char *);

/*
 * Creates node with the provided data
 * Inserts the node into the correct position in the tree
 * NOTE: Copy the data into the node
 */
void insertPhoneBook(PhoneBookP, char *, char *);

/*
 * Removes the node containing the matching names
 * Parameters: phonebook, firstname
 * Returns true if successful, else false
 *
 * NOTE: THIS FUNCTION IS FOR BONUS POINTS
 * YOU CAN SIMPLY INSERT A DUMMY FUNCTION THAT 
 * ALWAYS RETURNS ZERO IF YOU CHOOSE
 */
int removePhoneBook(PhoneBookP, char *);

/*
 * Dislpays all the entries in the Phone book in order
 * Display one person per line, firstname followed by phone number
 */
void displayPhoneBook(PhoneBookP);

/*
 * Frees the memory used by each node in the book
 * Frees the memory used by this addressbook
 */
void freePhoneBook(PhoneBookP);

#endif

继承人.c文件

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "PhoneBook.h"
typedef struct NodeT
{
    struct NodeT *Left;
    struct NodeT *Right;
    char *Name;
    char *Number;
}* NodeP;

struct PhoneBookT
{
    NodeP Root;
};
static void Insert(PhoneBookP, NodeP, char* Name,char* Number);
static void traversePrint(NodeP N);
static NodeP newNode(PhoneBookP P, NodeP, char *Name,char *Number);
static int find(NodeP N,char *Name);
static void traverseFree(NodeP N);
PhoneBookP newPhoneBook()
{
    PhoneBookP P =(PhoneBookP) malloc(sizeof(PhoneBookP));
    P->Root = NULL;
    return P;
}
void lookupPhoneBook(PhoneBookP P, char * Name)
{
    if(find(P->Root, Name));
    else printf("Error\n");
}
static int find(NodeP N,char *Name)
{
    if(N)
    {
        find(N->Left,Name);
        if(0 == strcmp(Name,N->Name))
        {
            printf("Name: %s\nNumber: %s\n", N->Name, N->Number);
            return 1;
        }
        find(N->Right,Name);
    }
    else
        return 0;
}
void insertPhoneBook(PhoneBookP P, char *Name, char *Number)
{
    if(P->Root)
        Insert(P, P->Root, Name,Number);
    else
        P->Root = newNode(P,P->Root, Name,Number);
}
static void Insert(PhoneBookP P,NodeP N,  char* Name,char* Number)
{
    if(N)
    {
        if(0 > strcmp(Name,N->Name))
        {
            if(N->Left)
            {
                Insert(P,N->Left, Name, Number);
            }
            else
            {
                N->Left = newNode(P,N->Left, Name, Number);
            }
        }
        else
        {
            if(N->Right)
            {
                Insert(P,N->Right, Name, Number);
            }
            else
            {
                N->Right = newNode(P,N->Right, Name, Number);
            }
        }
    }
    else
        N = newNode(P, N, Name, Number);
}
static NodeP newNode(PhoneBookP P,NodeP N,char *Name,char *Number)
{
    NodeP New = (NodeP) malloc(sizeof(NodeP));
    N = New;
    New->Left = NULL;
    New->Right = NULL;
    New->Name = Name;
    New->Number = Number;
    return New;
}
int removePhoneBook(PhoneBookP P, char * Name)
{
    return 0;
}
void displayPhoneBook(PhoneBookP P)
{
    traversePrint(P->Root);
}
static void traversePrint(NodeP N)
{
    if(N)
    {
        traversePrint(N->Left);
        printf("Name: %s\n", N->Name);
        printf("Number: %s\n", N->Number);
        traversePrint(N->Right);
    }
}
void freePhoneBook(PhoneBookP P)
{
    traverseFree(P->Root);
}
static void traverseFree(NodeP N)
{
    if(N)
    {
        traverseFree(N->Left);
        traverseFree(N->Right);
        free(N);
    }
}

这是测试人员 我没有制作删除功能,所以不要使用它。

/*
 * PhoneBookTest.h
 * Cop 3411 Spr11
 * jlewis
 */

#include "PhoneBook.h"
#include <stdio.h>

int main()
{
    PhoneBookP myBook = newPhoneBook();
    printf("Book contains (Joe, Sue, Tom, Vince, Zachary)\n");
    insertPhoneBook(myBook, "Sue", "800-444-4444");
    insertPhoneBook(myBook, "Joe", "555-5555");
    insertPhoneBook(myBook, "Tom", "111-1111");
    insertPhoneBook(myBook, "Zachary", "1-888-888-8888");
    insertPhoneBook(myBook, "Vince", "333-3333");
    displayPhoneBook(myBook);
    printf("\nLooking for Sue ... ");
    lookupPhoneBook(myBook, "Sue");
    printf("Looking for Tom ... ");
    lookupPhoneBook(myBook, "Tom");
    printf("Looking for Zac ... ");
    lookupPhoneBook(myBook, "Zachary");
/*
    fprintf(stderr, "\nRemoving Joe\n");
    removePhoneBook(myBook, "Joe");
    displayPhoneBook(myBook);
*/
    printf("\nAdding 5 more ... Al, Jason, Thomas, Billy, Tommy\n");
    insertPhoneBook(myBook, "Al", "888-8888");
    insertPhoneBook(myBook, "Jason", "888-8888");
    insertPhoneBook(myBook, "Thomas", "888-8888");
    insertPhoneBook(myBook, "Billy", "888-8888");
    insertPhoneBook(myBook, "Tommy", "888-8888");
    displayPhoneBook(myBook);
   /*
    fprintf(stderr, "\nRemoving Thomas\n");
    //removePhoneBook(myBook, "Thomas");
    displayPhoneBook(myBook);
    fprintf(stderr, "\nRemoving Zachary\n");
    //removePhoneBook(myBook, "Zachary");
    displayPhoneBook(myBook);*/
    freePhoneBook(myBook);
    return 0;
}

任何帮助非常感谢

这也应该在中部时间下午4点到期。

谢谢!

1 个答案:

答案 0 :(得分:1)

一个主要的错误是,当您通过sizeof(PhoneBookP)sizeof(struct PhoneBookT)时,您将sizeof(*P)传递给malloc。