所以我的问题是,当我从数组中的一个成员的字符串中分配内存时,内存会损坏,我不知道为什么会这样。
我的主要是
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define maxsize 512
#define charsize 100
typedef struct Books {
char* id;
char* title;
char* author;
char* pages;
char* year;
char* subject;
} book;
char* filename;
int libsize=4;
int bookcount=1;
void printbooks(book books[]);
void printbooksf(book books[]);
void srchbook(book books[]);
void delbook(book books[]);
void pick(book books[]);
void addbook(book books[]);
void addbookf(book books[]);
int main(int argc, char* argv[]){
if (argc < 1)
return -1;
filename=argv[1];
FILE* fptr;
char tempstring[maxsize],* token;
int i=0,ch;
book *books=NULL;
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);
//starting size for the pointer
for(i=0;i<bookcount;i++){
books[i].id=(char*)malloc(charsize);
books[i].title=(char*)malloc(charsize);
books[i].author=(char*)malloc(charsize);
books[i].pages=(char*)malloc(charsize);
books[i].year=(char*)malloc(charsize);
books[i].subject=(char*)malloc(charsize);
}
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,",");
strcpy(books[i].id,token);
token=strtok(NULL,",");
strcpy(books[i].title,token);
token=strtok(NULL,",");
strcpy(books[i].author,token);
token=strtok(NULL,",");
strcpy(books[i].pages,token);
token=strtok(NULL,",");
strcpy(books[i].year,token);
token=strtok(NULL,",");
strcpy(books[i].subject,token);
}
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);
return 1;
}
现在我从印刷本上得到的输出是
Treasure Island
Heir to The Empire
Plumbing for Dummies
Berserk(!!!!)
The Troll Cookbook : Human Delights
Funny Cats
Linus the Vegetarian T. rex
Algebra 3
The Hitchhiker's Guide to the Galaxy
当我使用addbook和printbook时,其中一个成员是如何被破坏的
Treasure Island
x-
Plumbing for Dummies
Berserk(!!!!)
The Troll Cookbook : Human Delights
Funny Cats
Linus the Vegetarian T. rex
Algebra 3
The Hitchhiker's Guide to the Galaxy
the book i added
地址簿是
void addbook(book books[]){
char tempstring[maxsize];
char bugfixer;//gets the /n instead of the temp string
bugfixer=getc(stdin);
int i;
++bookcount;
if(libsize<bookcount){
while(libsize < bookcount){
libsize*=1.5;}
books=realloc(books,libsize);
}
if(books==NULL){
printf("not enough space\n");
exit(-1);}
for(i=bookcount-1;i<bookcount;i++){
books[i].id=(char*)malloc(charsize);
books[i].title=(char*)malloc(charsize);
books[i].author=(char*)malloc(charsize);
books[i].pages=(char*)malloc(charsize);
books[i].year=(char*)malloc(charsize);
books[i].subject=(char*)malloc(charsize);
}
for(i=bookcount-1;i<bookcount;++i){
printf("add the id\n");
gets(tempstring);
strcpy(books[i].id,tempstring);
printf("add the title\n");
gets(tempstring);
strcpy(books[i].year,tempstring);
printf("add the author\n");
gets(tempstring);
strcpy(books[i].title,tempstring);
printf("add the pages\n");
gets(tempstring);
strcpy(books[i].pages,tempstring);
printf("add the year\n");
gets(tempstring);
strcpy(books[i].author,tempstring);
printf("add the subject\n");
gets(tempstring);
strcpy(books[i].subject,tempstring);
}
printf("book number %d added",bookcount);
printf("\n");
pick(books);
}
`
印刷本是
void printbooks(book books[]){
int i;
for(i=0;i<bookcount;i++){
printf("%s\n",books[i].title);
}
printf("\n");
pick(books);
}
现在我试图理解的是为什么只有第二个成员会被破坏
为什么任何成员都会被破坏
最佳答案
评论中似乎还指出了其他问题。
但正如您在调用addbook
时提到的行为一样,这种行为与释放内存有关:
在addbook(book books[])
内,您可以realloc
参数books
(即books=realloc(books,libsize)
。因此,最有可能释放参数books
指向的内存,并分配新的内存位置。但是,请注意,传递给函数addbook
的变量仍将指向“old”,并在同时释放的内存中。
如果realloc
是一个参数,那么该参数应该是指向指针的指针,即book **book
,这样代码调用函数addbook
也将获得指向新内存块的指针。必须相应地更改addbook
中的代码。
关于c - 在结构中带有动态数组的预期输出,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43194092/