0

I'm trying to figure out where my problem is and can't seem to find it, I want to a allocate dynamic memory to a variable within a struct, I have tried in all sorts of ways and it just does not work I get a warning: Exception has occurred. segmentation fault

When I allocate memory to the *bookcase its works but when i try to allocate to a variable within the structure i get the warning.

example of what I was trying to do

struct book
 {
  int num;
  char* book_name;
 }*bookcase;

void addbook(void)
 {
  char buff[20];
  gets(buff);
  bookcase->book_name = ( char *)malloc(strlen(buff));
  strcpy(bookcase->book_name,buff);
 }

Sorry for the question feels really stupid, unfortunately i could not understand the problem, Any help would be appreciated. Thank you.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • 1
    Don't even move to dynamic allocation until you understand [Why gets() is so dangerous it should never be used!](https://stackoverflow.com/q/1694036/3422102) In C, there is no need to cast the return of `malloc`, it is unnecessary. See: [Do I cast the result of malloc?](http://stackoverflow.com/q/605845/995714) – David C. Rankin Apr 10 '21 at 20:05
  • Post the input used for `gets(buff);` – chux - Reinstate Monica Apr 10 '21 at 20:44

2 Answers2

1

You're not allocating enough memory.

A string in C is terminated by a null byte. The amount of space you're allocating doesn't account for that null byte, so you write past the end of allocated memory when you copy the string. This triggers undefined behavior which in this case causes a crash.

Add space for the null byte when allocating:

bookcase->book_name = malloc(strlen(buff) + 1);
strcpy(bookcase->book_name,buff);

Or, if your system supports it, use strdup which does the allocation and copy in one step:

bookcase->book_name = strdup(buff);
dbush
  • 205,898
  • 23
  • 218
  • 273
  • May want to address the fact that `bookcase` is just an uninitialized pointer to type `struct book` so `bookcase->book_name` is Undefined Behavior (even before you get to the too short allocation). – David C. Rankin Apr 10 '21 at 20:08
  • @DavidC.Rankin The code that calls `addbook` isn't shown, and the question mentions allocating memory for `bookcase`, so there's an assumption that that was done correctly. – dbush Apr 10 '21 at 20:11
  • Good point. But based on what's shown that jumped out. – David C. Rankin Apr 11 '21 at 00:03
1
  1. you need to allocate memory for the struct itself
bookcase = malloc(sizeof(*bookcase));

then you need to allocate enough memory for the title. You do not allocate enough as string in C is terminated by the bull character and this nill char is not counted by the strlen

bookcase -> book_name = malloc(strlen(buff) + 1);

But you do not two allocations for that.

struct book
 {
  int num;
  char book_name[];
 };

struct book *addbook(void)
 {
  char buff[20];
  struct book *bookcase;
  fgets(buff, sizeof(buff), stdin); 
  /* you should check for errors in all I/O operations and memory allocations */
  bookcase = malloc(sizeof(*bookcase) + strlen(buff) + 1);
  strcpy(bookcase->book_name,buff);
  return bookcase;
 }

0___________
  • 60,014
  • 4
  • 34
  • 74