我有一个 int 指针数组。
int *ints[3];
定义这个数组后内存会发生什么变化?存储的初始指针值是多少?当我尝试取消引用一个值时,我得到一个 segmentation fault
,它告诉我这些值是任意的。
最佳答案
这取决于数组的存储。
如果它有静态存储,即在任何函数之外声明或 在函数中用
static
声明,则所有元素都将为 NULL如果在函数中声明,没有
static
,它将有 自动存储且初始值不确定
标准恰好在这件事上非常清楚并且很有帮助:
If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate. If an object that has static or thread storage duration is not initialized explicitly, then:
- if it has pointer type, it is initialized to a null pointer;
- if it has arithmetic type, it is initialized to (positive or unsigned) zero;
- if it is an aggregate, every member is initialized (recursively) according to these rules, and any padding is initialized to zero
bits;
关于c - c在第一次定义时如何给指针数组赋值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21840919/