简体   繁体   English

指针默认初始化值不为NULL?

[英]Pointers Default initialized Value is not NULL?

How do we know pointers are not initialized to NULL by default?我们怎么知道默认情况下指针没有初始化为 NULL? There is a similar questions directed at Why aren't pointers initialized with NULL by default?有一个类似的问题针对为什么默认情况下不使用 NULL 初始化指针? Just for checking, here is a very simple code just to see if a pointer is set to NULL by default.只是为了检查,这是一个非常简单的代码,只是为了查看默认情况下是否将指针设置为 NULL。

#include <iostream>
using namespace std;
int main()
{
    int* x;
    if(!x)
        cout << "nullptr" << endl;
    return 0;
}

and at the output, I received nullptr message.在输出中,我收到了 nullptr 消息。 I appreciate if someone can clarify this.如果有人能澄清这一点,我很感激。

How do we know pointers are not initialized to NULL by default?我们怎么知道默认情况下指针没有初始化为 NULL?

Because we know that the standard says that default initialised pointer has an indeterminate value if it has automatic or dynamic storage.因为我们知道标准说如果默认初始化指针具有自动或动态存储,则它具有不确定的值。 Quote from the standard (draft):引用自标准(草案):

[dcl.init] If no initializer is specified for an object, the object is default-initialized. [dcl.init] 如果没有为对象指定初始值设定项,则该对象是默认初始化的。 When storage for an object with automatic or dynamic storage duration is obtained, the object has an indeterminate value , and if no initialization is performed for the object, that object retains an indeterminate value until that value is replaced.当获取一个自动或动态存储期限的对象的存储时,该对象具有一个不确定值,如果没有对该对象进行初始化,该对象将保留一个不确定值,直到该值被替换。 ... ...

And further:并进一步:

[dcl.init] To default-initialize an object of type T means: [dcl.init] 默认初始化一个 T 类型的对象意味着:

— If T is a (possibly cv-qualified) class type [pointer isn't a class, so we don't care] — 如果 T 是(可能是 cv 限定的)类类型 [指针不是类,所以我们不关心]

— If T is an array type [pointer isn't an array, so we don't care] — 如果 T 是数组类型 [指针不是数组,所以我们不在乎]

— Otherwise, no initialization is performed. — 否则,不执行初始化。


I have declared a char (and also int) pointer without initializing it , and I got null pointers.我在没有初始化的情况下声明了一个 char(以及 int)指针,并且得到了空指针。

Reading an indeterminate value has undefined behaviour.读取不确定的值具有未定义的行为。 Quote from the standard (draft):引用自标准(草案):

[dcl.init] ... If an indeterminate value is produced by an evaluation, the behavior is undefined except in the following cases: [cases which don't apply here] [dcl.init] ... 如果评估产生不确定值,则行为未定义,但以下情况除外:[此处不适用的情况]

The question you linked to handles variables with local storage duration exclusively, so I assume you refer to these as well.您链接到的问题专门处理具有本地存储持续时间的变量,因此我假设您也参考了这些。

Such variables are not initialised if you don't do so yourself, so they get the value of whatever was written in their memory location before (standard wording: their value is 'indeterminate') – nothing speaks against, though, that this memory already is zero – by pure accident!如果您自己不这样做,则此类变量不会被初始化,因此它们会获得之前写入其内存位置的任何值(标准措辞:它们的值是“不确定的”)——不过,没有什么可以反对这个内存已经零-纯属偶然!

You can try the following:您可以尝试以下操作:

void test()
{
    int* p; // uninitialized
    std::cout << p << std::endl; // undefined behaviour!!!
    // (that's what you most likely did already...)

    // now something new: change the memory...
    p = reinterpret_cast<int*>(static_cast<uintptr_t(0xaddadaad));
}

int main()
{
    test();

    // again something new: call it a SECOND time:
    test();
}

As this is undefined behaviour there are no guarantees at all that you will get any meaningful output – chances are, though that the memory of first function call is reused in second one and you might get output ressembling to the following:由于这是未定义的行为,因此根本无法保证您将获得任何有意义的输出——尽管第一个函数调用的内存在第二个函数调用中被重用,并且您可能会得到类似于以下内容的输出:

00000000
addadaad

So even if there just happened to be all zero memory at programme start, it might differ from that at some later point while your programme is running...因此,即使在程序启动时碰巧内存为零,它也可能与您的程序运行时稍后的内存有所不同......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM