C99 中如何为隐式定义的多维数组分配内存? [英] How is memory allocated for an implicitly defined multidimensional array in C99?

查看:25
本文介绍了C99 中如何为隐式定义的多维数组分配内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个 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");

或者是每个字符串只分配了它在定义时需要的内存量(即stuff[0] 包含一个 6 元素的数组,stuff[1] 包含 4 个元素的数组,stuff[2] 包含 9 个元素的数组)?

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)?

推荐答案

图片可以提供帮助 - ASCII 艺术很有趣(但很费力).

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 | |
+---+---+---+---+---+---+---+---+---+

为二维数组分配的内存是连续的.x 表示未初始化的字节.请注意,stuff[0] 是指向 'hello' 的 'h' 的指针,stuff[1] 是指向 'pie' 的 'p' 的指针,并且 stuff[2] 是一个指向 'deadbeef' 的第一个 'd' 的指针(并且 stuff[3] 是一个不可解引用的指针,指向超出空值的字节'deadbeef' 之后的字节).

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').

图片完全不同.

请注意,您可以编写以下任何一个:

Note that you could have written either of these:

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

并且您将拥有与二维数组图中所示相同的内存布局(除了 x 将被清零).

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

这篇关于C99 中如何为隐式定义的多维数组分配内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆