C でツリーを作成しました。Free 関数を除いて、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);
}
}
これがThe Testerです。削除機能は作成していないので、使用しないでください。
/*
* 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 時に予定されています。
ありがとう!