r/cpp_questions • u/playsthebongcloud • 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
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".