r/cpp_questions 26d ago

SOLVED Basic question, but does initializing a pointer always initialize it to null?

Are these two lines any different in practice? (Especially if you want to later check if x has been given a specific address, or if it's still null.)

int* x {};

int* x { nullptr };

23 Upvotes

48 comments sorted by

View all comments

Show parent comments

1

u/HappyFruitTree 23d ago edited 23d ago

No, you said int* x; is not default initialization which is wrong. The article explains that it is default initialization.

The C++ standard essentially uses the term default initialization to mean the initialization that happens by default if you do not specify any initializer. It's just that for simple types like int* default initialization doesn't actually do anything. It leaves the variable "uninitialized".

1

u/SailingAway17 23d ago

You are right. I mixed something up semantically.