c - How do I copy a structure? -


i have problem have copy structure , add new memory free old memory. have increment create space new memory , add again. thought had logic correct keep getting segmentation fault. can't figure out going wrong.

here copy of structure:

struct childrensbook *book = (struct childrensbook *) malloc(sizeof(struct childrensbook));     //structure of book #1  book->title = (char *)malloc(100);                  //allows memmory of 100 characters                                                   book->author = (char *)malloc(100);             //allows memmory of 100 characters   book->publisher = (char *)malloc(100);              //allows memmory of 100 characters   book->copyright = 0; book->price = 0; 

here copy of add function:

int addrecord()  {     int headptr = 0;     struct childrensbook *book = malloc(sizeof(struct   childrensbook));            //get book structure     struct childrensbook *book1 = malloc(sizeof(struct childrensbook));         //create structure book1     memcpy(book->title, "we're going on bear hunt", 26);                      //populate fields of book      memcpy(book->author, "michael rosen", 13);                                  //populate fields of book      memcpy(book->publisher, "little simon", 12);                                //populate fields of book      book->copyright = 1989;                                                 //populate fields of book      book->price = 7.99;                                                     //populate fields of book      memcpy(book1, book, sizeof *book1);                     //copy fields of book book 1     free(book); } 

and here call function:

else if(x==4) {     addrecord();     fprintf(stderr, "you have added record: %s\n", book->title);     free(book);     moveptr++;   //here incrememnt new space. globaal variable } 

i see multiple errors here:

  • free(book) frees dynamically allocated struct book not dynamically allocated fields (book->title , such things must freed too)
  • you calling memcpy book->author , other char* fields these fields haven't been dynamically allocated, maybe wanted book->title = strdup("literal"),
  • memcpy(book1, book, sizeof *book1) has @ least 2 errors: first hare coping sizeof *book1 bytes, size of pointer, not whole struct. copying fields doesn't contain primitive types pointers share ownership between 2 books, should copy each single dynamically allocated field.

just give code start with:

void free_book(struct book* b) {   free(b->title);   free(b->author);   ...   free(b); }  struct* book dupe_book(struct book* source) {   struct book* dest = malloc(sizeof(struct book));   dest->price = source->price;   dest->author = strdup(source->author);   ... } 

Comments

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -