How is memory allocated for an implicitly defined multidimensional array in C99?

I am trying to write a C99 program, and I have an array of strings implicitly defined as such:

char *stuff[] = {"hello","pie","deadbeef"}; 

Since array sizes are not defined, how much memory is allocated for each row? Do all lines contain as many elements as the largest line in the definition? For example, will this following code be equivalent to the implicit definition above:

 char stuff[3][9]; strcpy(stuff[0], "hello"); strcpy(stuff[1], "pie"); strcpy(stuff[2], "deadbeef"); 

Or does each line only allocate the amount of memory that it needs during the definition (ie, stuff[0] contains an array of elements 6 , stuff[1] contains an array of 4 , and stuff[2] contains an array of elements 9 )?

+1
source share
4 answers

Pictures can help - ASCII Art is fun (but painstaking).

 char *stuff[] = {"hello","pie","deadbeef"}; +----------+ +---------+ | stuff[0] |--------->| hello\0 | +----------+ +---------+ +-------+ | stuff[1] |-------------------------->| pie\0 | +----------+ +------------+ +-------+ | stuff[2] |--------->| deadbeef\0 | +----------+ +------------+ 

The memory allocated for the 1D array of pointers is contiguous, but there is no guarantee that the pointers held in the array point to adjacent sections of memory (therefore, the lines of pointers of different lengths).

 char stuff[3][9]; strcpy(stuff[0], "hello"); strcpy(stuff[1], "pie"); strcpy(stuff[2], "deadbeef"); +---+---+---+---+---+---+---+---+---+ | h | e | l | l | o | \0| x | x | x | +---+---+---+---+---+---+---+---+---+ | p | i | e | \0| x | x | x | x | x | +---+---+---+---+---+---+---+---+---+ | d | e | a | d | b | e | e | f | \0| +---+---+---+---+---+---+---+---+---+ 

The memory allocated for the 2D array is contiguous. X stands for uninitialized bytes. Note that stuff[0] is a pointer to 'h' 'hello', stuff[1] is a pointer to 'p' 'pie', and stuff[2] is a pointer to the first 'd' of deadbeef '(and stuff[3] is a non-moving pointer to byte after zero byte after "deadbeef").

The images are pretty, completely different.

Please note that you could write any of them:

 char stuff[3][9] = { "hello", "pie", "deadbeef" }; char stuff[][9] = { "hello", "pie", "deadbeef" }; 

and you will have the same memory layout as shown in the 2D array diagram (except that x will be reset to zero).

+10
source
 char *stuff[] = {"hello","pie","deadbeef"}; 

Not a multidimensional array! It is just an array of pointers.

How much memory is allocated for each row?

Number of characters plus zero delimiter. Same as any string literal.

I think you want this:

 char foo[][10] = {"hello","pie","deadbeef"}; 

Here 10 is the amount of space per line, and all lines are in continuous memory. Thus, there will be an addition for lines smaller than size 10.

+4
source

In the first example, this is a jagged array.

It declares an array of constant pointers to char. So a string literal can be as long as you like. The row length is independent of the columns of the array.

In the second case, the number of characters in the string should be 9, as indicated by the size of your column or less.

+2
source

Do all rows have the same number of elements, such as the largest row in the definition?

No, only 3 pointers are highlighted, and they point to 3 string literals.

 char *stuff[] = {"hello","pie","deadbeef"}; 

and

 char stuff[3][9]; 

not at all equivalent. At first it is an array of 3 pointers, and the second is a 2D array.

For the first highlighted pointer, and the string literals that they point to can be stored in a read-only section. The second is allocated during automatic storage (usually on the stack).

0
source

Source: https://habr.com/ru/post/1202200/


All Articles