2

I found this one line that I don't understand. It says

char (*storage)[15] = malloc(sizeof *storage * 8)

Does anybody know what this is mean? why I see a lot of *?

I don't get it because why he/she multiplied by 8 (it seems like that) but then declared it was [15] too?

Correct me if I'm wrong.

5
  • 1
    cdecl.ridiculousfish.com/?q=char+%28*storage%29%5B15%5D (bit.ly/1WpCOWp) Commented Oct 31, 2015 at 15:48
  • 1
    @ForceBru you read my mind. Commented Oct 31, 2015 at 15:48
  • 1
    In other words, storage is now an array of 8 elements, each of which is an array of 15 chars. Commented Oct 31, 2015 at 15:51
  • @ForceBru & Erik: It says syntax error bad character "=" :( Commented Oct 31, 2015 at 15:56
  • @John, this site is for declaration deciphering only Commented Oct 31, 2015 at 15:59

3 Answers 3

3
char (*storage)[15] = malloc(sizeof *storage * 8)

It allocates memory for 8 character arrays of size 15 .

For all * you ask-

char (*storage)[15]              // pointer to array of 15 chars 

and this -

sizeof *storage * 8   // this is 8 times sizeof type to which storage points 
Sign up to request clarification or add additional context in comments.

10 Comments

Ahh I see. Is there any alternative (much simpler) way to define it?
@John Yes , you can write like this -char storage[8][15];.
Now that seems familiar to me! Thank you.
@ameyCU And how do you free() that?
char *storage = malloc(15*8) is not the same as char (*storage)[15] = malloc(sizeof *storage * 8). Although the same amount of memory is allocated, storage behaves completely different in case pointer arithmetics are applied to it.
|
1

char (*storage)[15] is a pointer to 15-element array of char.

sizeof *storage * 8 is 8 times the size of the type at which storage points.

1 Comment

Ah I see. Is there any alternative (much simpler) way to define it?
0
char (*storage)[15] = malloc(sizeof *storage * 8);

dynamically allocates an 8x15 array of char. This is roughly similar to writing

char storage[8][15];

except that

  • The lifetime of a dynamically-allocated object lasts until it is explicitly deallocated with free, whereas the lifetime of an automatically-allocated object lasts until the program exits the object's enclosing scope;
  • The storage for a dynamically-allocated object is (usually) taken from a different memory segment than automatically-allocated objects;
  • You can extend the size of a dynamically-allocated array as necessary. IOW, if you realized you needed two more rows, you could write
    size_t curRows = 8;
    char (*tmp)[15] = realloc( storage, sizeof *storage * (curRows + 2));
    if ( tmp )
    {
      storage = tmp;
      curRows += 2;
    }
    
    You can't do that with an automatically-allocated array.

An alternate way of dynamically allocating an 8x15 array is something like this:

char **storage = malloc( sizeof *storage * 8 );
if ( storage )
{
  for ( size_t i = 0; i < 8; i++ )
  {
    storage[i] = malloc( sizeof *storage[i] * 15 );
  }
}

Note that all three versions can be indexed as storage[i][j].

The main advantage of the last method is that each row can be different lengths if you want them to be. The main disadvantages are that the rows won't necessarily be adjacent in memory, and you have to free each storage[i] before you can free storage.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.