gpt4 book ai didi

c - 关于结构体的动态分配数组并在函数之间发送它

转载 作者:行者123 更新时间:2023-11-30 16:47:38 25 4
gpt4 key购买 nike

我有一些问题,我想知道我做错了什么,是否有人可以向我解释有关 realloc 和 malloc 函数的更多详细信息。我遇到的问题来 self 的主要问题:

typedef struct Books {
char *id;
char *title;
char *author;
char *pages;
char *year;
char *subject;
} book;
char* filename;
int bookcount=1;
int libsize=4;
int main(int argc, char* argv[]){
if (argc < 2)
return -1;
filename=argv[1];
FILE* fptr;
book* books;
char tempstring[maxsize],* token;
int i=0,ch;
fptr=fopen(filename,"r");
if(fptr==NULL)
return-1;
//this count how many books are in the file
while(ch!= EOF){
ch=fgetc(fptr);
if(ch == '\n')
++bookcount;
}
fclose(fptr);
while(libsize<bookcount){
libsize *= 1.5;
}
books=(book*) malloc(libsize*sizeof(book));
if(books==NULL)
exit(-1);

fptr=fopen(filename,"r");
if(fptr==NULL)
return-1;
//this gets all the books into the book array
for(i=0;i<bookcount;i++){
fgets(tempstring,maxsize,fptr);
token=strtok(tempstring,",");
ch=strlen(token);
books[i].id=(char*)malloc(ch+1);
strcpy(books[i].id,token);
token=strtok(NULL,",");
ch=strlen(token);
books[i].title=(char*)malloc(ch+1);
strcpy(books[i].title,token);
token=strtok(NULL,",");
ch=strlen(token);
books[i].author=(char*)malloc(ch+1);
strcpy(books[i].author,token);
token=strtok(NULL,",");
ch=strlen(token);
books[i].pages=(char*)malloc(ch+1);
strcpy(books[i].pages,token);
token=strtok(NULL,",");
ch=strlen(token);
books[i].year=(char*)malloc(ch+1);
strcpy(books[i].year,token);
token=strtok(NULL,",");
ch=strlen(token);
books[i].subject=(char*)malloc(ch+1);
strcpy(books[i].subject,token);
}
books=(book*) realloc(books,libsize*sizeof(book));
fclose(fptr);
printf("to add a book press 1\n");
printf("to delete a book press 2\n");
printf("to find a book press 3\n");
printf("to print all books press 4\n");
printf("to save library in a file press 5\n");
printf("to add books from a file press 6\n");
printf("to exit press 0\n");
pick(books);
free(books);
return 1;
}

我将动态分配的结构数组发送到一个函数中,让我选择现在当我打电话打印书籍时

void printbooks(book* books){
int i;
for(i=0;i<bookcount;++i){
printf("%s\n",books[i].title);
}
printf("Fin\n");
pick(books);
}

我得到了预期的输出当我使用 addbook 函数时如何

void addbook(book* books){
char tempstring[maxsize];
gets(tempstring);
book* temp;
int i=bookcount-1,ch;
++bookcount;
if(libsize < bookcount){
while(libsize < bookcount){
libsize*=1.5;}
temp=(book*)realloc(books,libsize);
}
if(temp==NULL){
printf("no more space\n");
exit(-1);}

if(temp!=NULL){
books=temp;}

printf("add the id\n");
gets(tempstring);
ch=strlen(tempstring);
books[i].id=(char*)malloc(ch+2);
strcpy(books[i].id,tempstring);
printf("add the title\n");
gets(tempstring);
ch=strlen(tempstring);
books[i].title=(char*)malloc(ch+2);
strcpy(books[i].title,tempstring);
printf("add the author\n");
ch=strlen(tempstring);
books[i].author=(char*)malloc(ch+2);
gets(tempstring);
strcpy(books[i].author,tempstring);
printf("add the pages\n");
gets(tempstring);
ch=strlen(tempstring);
books[i].pages=(char*)malloc(ch+2);
strcpy(books[i].pages,tempstring);
printf("add the year\n");
gets(tempstring);
ch=strlen(tempstring);
books[i].year=(char*)malloc(ch+2);
strcpy(books[i].year,tempstring);
printf("add the subject\n");
gets(tempstring);
ch=strlen(tempstring);
books[i].subject=(char*)malloc(ch+2);
strcpy(books[i].subject,tempstring);

printf("book number %d added",bookcount);
printf("\n");
pick(books);
}

一些结构成员被损坏,程序崩溃,当我尝试将库大小更改为 20,然后在我尝试输入新书的第一个成员时运行它时,程序崩溃了。

最佳答案

addbook

void addbook(book* books){
..
libsize*=1.5;}
temp=(book*)realloc(books,libsize);
}

realloc 调用未正确调整数组大小(您可以在另一个函数中正确执行此操作)。它应该包括字体大小:

temp = realloc(books,libsize*sizeof(book));

否则它会缩小大小,并且您会出现损坏/未定义的行为,因为例如内存会在其他地方重用。

一般注意事项:这些分配函数很棘手且容易出错:最好将它们放在实用函数中并且仅使用函数,不要复制/粘贴此类代码一百万次(例如: malloc(ch+2) 分配 1 个字节太多,很多次)。对于您的代码,可以使用审查。

关于c - 关于结构体的动态分配数组并在函数之间发送它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43254383/

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