c - regarding dynamic allocation array of a structs and sending it between functions -


i have few issues wondering doing wrong , if can explain me futher details realloc , malloc functions. problem have main:

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 in 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 books 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 book press 1\n");     printf("to delete book press 2\n");     printf("to find book press 3\n");     printf("to print books press 4\n");     printf("to save library in file press 5\n");     printf("to add books file press 6\n");     printf("to exit press 0\n");     pick(books);     free(books);     return 1; } 

i send array of dynamicly allocated structs funtction let me pick when call print books

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

i expected output how ever when use function 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 id\n");     gets(tempstring);     ch=strlen(tempstring);     books[i].id=(char*)malloc(ch+2);     strcpy(books[i].id,tempstring);     printf("add title\n");     gets(tempstring);     ch=strlen(tempstring);     books[i].title=(char*)malloc(ch+2);     strcpy(books[i].title,tempstring);     printf("add author\n");     ch=strlen(tempstring);     books[i].author=(char*)malloc(ch+2);     gets(tempstring);     strcpy(books[i].author,tempstring);     printf("add pages\n");     gets(tempstring);     ch=strlen(tempstring);     books[i].pages=(char*)malloc(ch+2);     strcpy(books[i].pages,tempstring);     printf("add year\n");     gets(tempstring);     ch=strlen(tempstring);     books[i].year=(char*)malloc(ch+2);     strcpy(books[i].year,tempstring);     printf("add 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); } 

some of struct members gets corrupted , program crash, when tried change library size 20 , run once tried enter first member of new book program crashed.

in addbook

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

the realloc call not resize array (you in function). should include type size:

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

otherwise shrinks size , corruption/undefined behaviour because memory reused somewhere else instance.

general note: allocation functions tricky , error prone: better put them in utility functions , use functions, don't copy/paste such code million times (ex: malloc(ch+2) allocates 1 byte much, lot of times). in case of code, use review.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -