<分区>
因为没有分配任何值的静态变量声明进入代码的 BSS 部分。它将自动初始化为零。
问题: 假设,如果在没有任何赋值的情况下声明静态指针,那么它的地址或值是否应该为零?
<分区>
因为没有分配任何值的静态变量声明进入代码的 BSS 部分。它将自动初始化为零。
问题: 假设,如果在没有任何赋值的情况下声明静态指针,那么它的地址或值是否应该为零?
最佳答案
在 C 中,静态指针将被初始化为 null,draft C99 standard 6.7.8
Initialization 段 10 说:
an object that has static storage duration is not initialized explicitly, then:
并包含以下项目符号:
— if it has pointer type, it is initialized to a null pointer;
所以没有为它分配存储空间,它是一个空指针。另请注意,这是一个实现定义的行为,其中存储了静态变量。
the C++ draft standard 的相关部分将是 8.5
Initializers 段 13 ,其中说(强调我的):
If no initializer is specified for an object, the object is default-initialized; if no initialization is performed, an object with automatic or dynamic storage duration has indeterminate value. [ Note: Objects with static or thread storage duration are zero-initialized, see 3.6.2. —end note ]
zero-initialize 包含在 6 段中,它说:
To zero-initialize an object or reference of type T means:
并具有以下项目符号:
— if T is a scalar type (3.9), the object is set to the value 0 (zero), taken as an integral constant expression, converted to T;103
footnote 103
说(强调我的):
As specified in 4.10, converting an integral constant expression whose value is 0 to a pointer type results in a null pointer value.
关于c++ - c/c++中的静态指针默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21964120/