Why Aren't Pointers Initialized With Null by Default

Why aren't pointers initialized with NULL by default?

We all realize that pointer (and other POD types) should be initialized.

The question then becomes 'who should initialize them'.

Well there are basically two methods:

  • The compiler initializes them.
  • The developer initializes them.

Let us assume that the compiler initialized any variable not explicitly initialized by the developer. Then we run into situations where initializing the variable was non trivial and the reason the developer did not do it at the declaration point was he/she needed to perform some operation and then assign.

So now we have the situation that the compiler has added an extra instruction to the code that initializes the variable to NULL then later the developer code is added to do the correct initialization. Or under other conditions the variable is potentially never used. A lot of C++ developers would scream foul under both conditions at the cost of that extra instruction.

It's not just about time. But also space. There are a lot of environments where both resources are at a premium and the developers do not want to give up either.

BUT: You can simulate the effect of forcing initialization. Most compilers will warn you about uninitialized variables. So I always turn my warning level to the highest level possible. Then tell the compiler to treat all warnings as errors. Under these conditions most compilers will then generate an error for variables that are un-initialized but used and thus will prevent code from being generated.

C++ default constructor does not initialize pointer to nullptr?

Pointers are "POD types"...a.k.a. "Plain Old Data". The rules for when and where they are default-initialized are summarized here:

Default initialization of POD types in C++

So no. It doesn't matter what your constructor for a class is, if it's a raw pointer as a member of the class. You aren't actually instantiating the class. So members like Foo * or std::vector<Foo> * or anything ending in * will not be initialized to nullptr.

The smart pointer classes are not POD. So if you use a unique_ptr<Foo> or a shared_ptr<Foo> that is creating instances of classes, that do have a constructor that makes them effectively null if you do not initialize them.

Does it matter if I do MyCLass* o = new MyCLass; or I do MyCLass* o = new MyCLass(); in C++11?

One question per question, please.

Do the parentheses after the type name make a difference with new?

why c++ doesn't have default initialization

I think most likely it is due to possible overhead of initializing these variables. This might not be such a big problem for one variable, but imagine arrays allocated on the stack.

c++ pointer never initialized still outputs as initialized

You do not initialise pHead or pTail in constructor or in place. So, these members will have arbitrary values when you create a LinkedList object. So, either write the constructor:

LinkedList()
:pHead(NULL), pTail(NULL)
{
}

Or initialize them in-place if you are using c++11:

private:    
pNode pHead = nullptr;
pNode pTail = nullptr;

Initializing a pointer in C++?

int *vector1 = NULL;
if (vector1 == NULL)
{

}

will work fine

int *vector1 = nullptr;
if (vector1 == nullptr)
{

}

Also works if you want to be a bit more up to date.

Pointers are not set to NULL by default. The answer why is here:
Why aren't pointers initialized with NULL by default?

Uninitialized vector pointer is not NULL

Uninitialized pointer has undetermined behaviour. Could be NULL or not, depending on the compiler (and, even for a specific compiler, Release and Debug binaries may have a different behaviour) and/or the memory state (see comments to this post).

It is recommended to always initialize them. Never expect an uninitialized variable to have a specific value (it's true for pointers, but also true for other types like int, bool that may take different default initialization values depending on the compiler/target).

It's actually hard to know what will be the value of an unitialized variable, and in many cases it is not deterministic.



Related Topics



Leave a reply



Submit