0

I have the following C struct:

typedef struct {

    char *name;
    int nfollowers;
    char *followers[];
} User;

There's a point in my code where I have to allocate memory for the last variable of the struct (followers) and I'm having issues on it.

I have tried this:

users[nusers].followers = (char **) realloc(users[nusers].followers, sizeof(char));

or this

users[nusers].followers = (char **) realloc(users[nusers].followers, sizeof(char *));

but the output I get after compiling is the following:

error: invalid use of flexible array member

How can I properly do this?

EDIT

Example of how my C file is structured:

 User *users;
 int i=0, n, nusers=0;
 char aux, *str;
 
 fd_in = open(argv[1], O_RDONLY);
 
 if (fd_in >= 0) {

    users = (User *) malloc(sizeof(User));

    while (aux!='&') {

        users[nusers].followers = (char **) realloc(users[nusers].followers, sizeof(char)); //Issue

          while (aux != '#') {
              ...
          }
      }
  }
17
  • 1
    This should probably be just char** followers. Where's the original malloc or calloc? Commented Sep 26, 2020 at 1:06
  • 2
    Please provide a minimal reproducible example. Including creation of users. Commented Sep 26, 2020 at 1:07
  • 2
    Was it your intention to make followers a flexible array member? Or was it supposed to be a normal array member? Commented Sep 26, 2020 at 1:22
  • 2
    realloc acts like mailloc if the pointer is NULL. When working with a FAM, you allocate for the struct and FAM at the same time. Currently your FAM is an array of pointers to char. So you need to know how many pointers you want when you allocate for your struct. For example, if you want 10 pointers, you would allocate User *users = calloc (1, sizeof *users + 10 * sizeof *users->followers); There can only be one FAM, so you cannot have an array of User, and the User stuct cannot be nested within another struct. Commented Sep 26, 2020 at 1:45
  • 1
    FAM Example trivially using string-literals as storage for followers. Additionally, In C, there is no need to cast the return of malloc, it is unnecessary. See: Do I cast the result of malloc? Commented Sep 26, 2020 at 2:04

1 Answer 1

1

As mentioned by tadman, instead of char *followers[]; use char **followers; to declare the field.

Also watch out that malloc does not initialize memory (though on linux the memory might be initialized to 0 if it has not been reused) so your use of realloc may result in corrupting the heap. Instead, just use malloc again (or use calloc to allocate the struct).

Sign up to request clarification or add additional context in comments.

2 Comments

I'm not sure if calling realloc with a wild pointer is likely to corrupt the heap. My guess is that it is much more likely to cause a crash (segmentation fault or access violation).
Same thing, really: just a matter of when that crash happens. Sometimes it's in the call to realloc, other times it might be in a call to free or malloc. This is why debugging such things can be very difficult.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.