Raw pointer is automatically initialized to nullptr?

Hello.

I'm making a List class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
template<typename T>
struct Node
{
	T val;
	Node* next;
};

template<typename T>
class GList
{
private:
	Node<T>* il;
	size_t items = 0;
public:
        // And this is the copy constructor
        GList(const GList<T>& copy)
        {
             cout << this->il;
        }
        
        ...


That cout prints 00000000 (nullptr)

I would like to know why the raw pointer il was initialized if I didn't request it.
Last edited on
Hi,

Is it this?

http://en.cppreference.com/w/cpp/language/zero_initialization

Not sure why you are worried about it, it has to be initialised to something - it may as well be nullptr as opposed to garbage.
I want to understand WHY the zero initialization took place there.
Without an actual program, we can only guess whether the output was produced by a program that initialized that pointer to zero (by the above-mentioned zero-init) or simply got zero as a perfectly reasonable garbage value to be found in memory.
I tested that very simply:

1
2
3
GList<int> my;

GList<int> my2{ my };


I updated the Copy Ctor putting this
1
2
3
4
5

if (il == nullptr)
	cout << "nullptr";
else
	cout << il;


The code prints nullptr.

I know Bjarne said that C++ dosn't zero-initializes everything because of performance: "why should one pay for something he didn't ask for?"

Well...
Still, not enough information to tell whether you're zero-initializing or default-initializting.

Here's your code snippets above combined in a program that zero-initializes
http://coliru.stacked-crooked.com/a/38d00ba8a9fcfd23

Here's your code snippets above combined in a program that default-initializes:
http://coliru.stacked-crooked.com/a/d3e0498b5b528ed4
Oh no, the GList objects are declared inside the main(), so they (should) not be zero-initialized
Edit: didn't see the previous 3 posts while doing mine :+)

I want to understand WHY the zero initialization took place there.


Did you read the documentation? There are other types of initialisation, all documented on cppreference.

If you didn't initialise it to something (nullptr), what were you expecting? How does it being garbage ( like you must have been expecting) help you? Would it not make sense to initialise it to nullptr? Then that means it isn't pointing to anything, and you could make use of that in a test condition.

Could you use a smart pointer instead of a raw pointer?
Last edited on
Did you read the documentation? There are other types of initialisation, all documented on cppreference.

None of the following matches my situation

1) For every named variable with static or thread-local storage duration that is not subject to constant initialization (since C++14), before any other initialization.
2) As part of value-initialization sequence for non-class types and for members of value-initialized class types that have no constructors.
3) When a character array is initialized with a string literal that is too short, the remainder of the array is zero-initialized.


If you didn't initialise it to something (nullptr), what were you expecting?

If I don't initialize something, I expect it to be garbage, not auto-initialized.

Could you use a smart pointer instead of a raw pointer?

No.
Last edited on
None of the following matches my situation


Doesn't number 1 apply to global variables: they have static storage duration?

If I don't initialize something, I expect it to be garbage, not auto-initialized.


If you are worried about the cost of that, I would have to say that is rather pedantic, IMO.

Could you use a smart pointer instead of a raw pointer?

No.


Why not? Aren't raw pointers supposed to be avoided in modern c++?
Garbage doesn't mean random. Zero is a possible "garbage value".

Modern operating systems often zero-initialize memory before giving it to a process to prevent the program from being able to read left over memory values from other programs because that could be a security concern.

I believe Visual C++ zero-initializes a lot of things that would otherwise be left uninitialized when compiling in debug mode. If you're using this compiler try compiling in release mode and see if there is a difference.
Last edited on
I believe Visual C++ zero-initialize a lot of things that would otherwise be left uninitialized when compiling in debug mode.

Yeah I'm using VC++ compiler.

If you're using this compiler try compiling in release mode and see if there is a difference.

I already compile in Release mode <.<
Last edited on
You've lost the battle of the optimizer. You class data member is not initialized. In fact, it's not even created or stored anywhere. VC++ just arbitrarily decided the uninitialized pointer it was asked to feed to std::cout was equal to the nullptr.

[edit: http://rextester.com/AYHHD15603 ]
Last edited on
You've lost the battle of the optimizer. You class data member is not initialized. In fact, it's not even created or stored anywhere. VC++ just arbitrarily decided the uninitialized pointer it was asked to feed to std::cout was equal to the nullptr.


I put the pointer inside an if-statement before couting it.

P.S: your link doesn't work
I put the pointer inside an if-statement before couting it.

It would be a poor optimizer that couldn't work around that.


P.S: your link doesn't work

I added a space for you.

Topic archived. No new replies allowed.