c++ - c/c++中的静态指针默认值

标签 c++ c static

<分区>

因为没有分配任何值的静态变量声明进入代码的 BSS 部分。它将自动初始化为零。

问题: 假设,如果在没有任何赋值的情况下声明静态指针,那么它的地址或值是否应该为零?

最佳答案

在 C 中,静态指针将被初始化为 null,draft C99 standard 6.7.8 Initialization10 说:

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 Initializers13 ,其中说(强调我的):

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/

相关文章:

c++ - 带有 C++ : Can't create a DBusGProxy, 的 DBus Glib 释放它并再次创建它

c++ - 需要高级低级编程的书籍和网站建议

c++ - SDL_image 不工作

c++ - Pthreads问题和几个问题

c - 如果比较两个指针的地址而不是内容,gcc/g++ 警告?

java - 基于声明为 "final static Object"的对象与简单的 "final Object"进行同步

C++ 静态虚拟成员?

c++ - C++ 中命名空间和友元的交互?

php - OOP 中静态方法存储在哪里?

c++ - 将一串多字节字符转换为宽字符会产生意想不到的结果