gpt4 book ai didi

c++ - 全局指针是否初始化为零?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:57:51 27 4
gpt4 key购买 nike

我想知道 cpp 标准对全局初始化是怎么说的。我找到了 this回答很有帮助,但没有提到指针类型。

能否保证这会奏效?

char* myptr
int main()
{
if (myptr == NULL)
{
std::cout << "All good!" << std::endl;
}
}

最佳答案

是的,在命名空间范围(在您的情况下为全局命名空间)定义的指针保证被初始化为该类型的正确空指针值。

对于标准引用,

3.6.2[basic.start.init]/2 "Variables with static storage duration ... shall be zero-initialized (8.5)"

8.5[dcl.init]/6 "To zero-initialize ... means: if T is a scalar type (3.9), the object is initialized to the value obtained by converting the integer literal 0 (zero) to T;[106]"

106) As specified in 4.10, converting an integer literal whose value is 0 to a pointer type results in a null pointer value.

(强调我的)

关于c++ - 全局指针是否初始化为零?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20788960/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com