在结构

时间:2017-04-03 20:55:22

标签: c arrays struct

所以我的问题是当我从一个字符串中的一个字符串中分配一个内存被损坏时我不知道为什么会这样。 我的主要是

#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

addbook是

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);
}
`

和printbook

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

现在我试图理解的是为什么只有第二个成员被破坏了 以及为什么任何成员都会被破坏

1 个答案:

答案 0 :(得分:2)

评论中指出似乎还有其他问题。 但是当你在调用addbook时引用行为时,这种行为与释放内存有关:

addbook(book books[])内,您realloc参数books(即books=realloc(books,libsize)。因此,参数books指向的内存最有可能被释放,并且但是,请注意,传递给函数addbook的变量仍将指向&#34; old&#34;以及同时释放的内存。

如果你realloc一个参数,那么参数应该是一个指向指针的指针,即book **book,这样代码调用函数addbook将得到一个指向新内存的指针块也是。 addbook中的代码必须相应更改。