gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 04:00:51 25 4
gpt4 key购买 nike

我用 C 做了一棵树,当我使用 Visual C++ 时,除了 free 函数外,一切都在我的电脑上工作。

当我在任何其他平台(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 处的堆 block 已在 00A01798 处修改,过去请求的大小为 28Windows 已在 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 点到期。

谢谢!

最佳答案

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

关于c - 释放内存/跨平台兼容性问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11693923/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com