Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
341 views
in Technique[技术] by (71.8m points)

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

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

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

Since the array dimensions are not defined, how much memory is allocated for each string? Are all strings allocated the same amount of elements as the largest string in the definition? For example, would 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 is each string allocated just the amount of memory it needs at the time of definition (i.e. stuff[0] holds an array of 6 elements, stuff[1] holds an array of 4 elements, and stuff[2] holds an array of 9 elements)?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Pictures can help — ASCII Art is fun (but laborious).

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

+----------+          +---------+
| stuff[0] |--------->| hello |
+----------+          +---------+      +-------+
| stuff[1] |-------------------------->| pie |
+----------+          +------------+   +-------+
| stuff[2] |--------->| deadbeef |
+----------+          +------------+

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 contiguous sections of memory (which is why the pointer lines are different lengths).

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

+---+---+---+---+---+---+---+---+---+
| h | e | l | l | o | | x | x | x |
+---+---+---+---+---+---+---+---+---+
| p | i | e | | x | x | x | x | x |
+---+---+---+---+---+---+---+---+---+
| d | e | a | d | b | e | e | f | |
+---+---+---+---+---+---+---+---+---+

The memory allocated for the 2D array is contiguous. The x's denote uninitialized bytes. Note that stuff[0] is a pointer to the 'h' of 'hello', stuff[1] is a pointer to the 'p' of 'pie', and stuff[2] is a pointer to the first 'd' of 'deadbeef' (and stuff[3] is a non-dereferenceable pointer to the byte beyond the null byte after 'deadbeef').

The pictures are quite, quite different.

Note that you could have written either of these:

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

and you would have the same memory layout as shown in the 2D array diagram (except that the x's would be zeroed).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...