2012-07-27 93 views
1

我在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或其中已加載的任何DLL的錯誤。

這可能也是由於用戶按下F12而PhoneBook.exe有焦點。

輸出窗口可能有更多診斷信息。 HEAP [PhoneBook.exe]:指定給RtlValidateHeap的無效地址(00A00000,00A01770) Windows已觸發PhoneBook.exe中的斷點。

這可能是由於堆的損壞,這表明PhoneBook.exe或其中已加載的任何DLL的錯誤。

這可能也是由於用戶按下F12而PhoneBook.exe有焦點。

輸出窗口可能有更多診斷信息。

這裏是我的所有代碼:

/* 
* 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點中心的時間。

謝謝!

+3

這是很多代碼。您應該通過使用調試器將其縮小到錯誤,並向我們展示一段更簡潔的代碼,該代碼仍會顯示錯誤。 – pmr 2012-07-27 18:55:20

+1

在調試器或valgrind下運行您的代碼以幫助識別問題並修復錯誤。 – 2012-07-27 19:00:07

+0

也請注意您的[上一個問題](http://stackoverflow.com/questions/11299723/assigning-a-pointer-in-a-struct-to-a-variable)中給出的建議,並且不要投射malloc的結果。 – 2012-07-28 06:47:46

回答

1

一個主要的錯誤是,當你通過sizeof(struct PhoneBookT)sizeof(*P)時,你傳遞sizeof(PhoneBookP)到malloc。

+0

'typedef struct PhoneBookT * PhoneBookP;'我相信這應該解決這個問題。 – PilHliP 2012-07-27 19:04:25

+0

Abd類似於'NodeP'的'malloc()'調用 – YePhIcK 2012-07-27 19:13:28

+0

當我給指針的sizeof指針時,出現語法錯誤。 – PilHliP 2012-07-27 19:17:26