0

How do we know pointers are not initialized to NULL by default? There is a similar questions directed at Why aren't pointers initialized with NULL by default? Just for checking, here is a very simple code just to see if a pointer is set to NULL by default.

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

and at the output, I received nullptr message. I appreciate if someone can clarify this.

Dev
  • 49
  • 5
  • If these were local variables it is undefined behavior. If they are static or global variables it is expected. Either way raw pointers should be used sparingly in modern `c++` – drescherjm Jan 14 '19 at 19:09
  • 1
    There's nothing that says that an unitialized pointer cannot be null. There's nothing it the standard that says that it _will_ be null either (in general). – Mat Jan 14 '19 at 19:10
  • 1
    In some contexts pointers do get initialized to `nullptr`. Right now is too generic to give an answer. – SergeyA Jan 14 '19 at 19:11
  • @Mat without code, it is impossible to say. There are cases when standard guarantees a pointer will be `nullptr`. – SergeyA Jan 14 '19 at 19:11
  • 2
    you cannot tell the difference between a value 0 due to initialization and a 0 due to no-initialization. In general its not a good idea to check the rules of c++ by trial and error. If some thing is undefined then you can get any outcome (typically just the one you expect until you make a demo for your boss) – 463035818_is_not_an_ai Jan 14 '19 at 19:13
  • 1
    Here is an initialized pointer to null: `char* p1 = nullptr;` And here is an uninitialized pointer: `char * p2;` – Eljay Jan 14 '19 at 19:13
  • Related: https://stackoverflow.com/questions/2218254/variable-initialization-in-c – drescherjm Jan 14 '19 at 19:21

2 Answers2

5

How do we know pointers are not initialized to NULL by default?

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. 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:

— If T is a (possibly cv-qualified) class type [pointer isn't a class, so we don't care]

— If T is an array type [pointer isn't an array, so we don't care]

— Otherwise, no initialization is performed.


I have declared a char (and also int) pointer without initializing it , and I got null pointers.

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]

Community
  • 1
  • 1
eerorika
  • 232,697
  • 12
  • 197
  • 326
1

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...

Aconcagua
  • 24,880
  • 4
  • 34
  • 59
  • 1
    This is a good point. Because of OS security reasons memory given to your application could be initially filled with 0. However that does not mean that you should make use of this. This answer shows how making use of this undefined behavior can and will cause unexpected results. – drescherjm Jan 14 '19 at 19:35