Is the NULL pointer same as an uninitialized pointer?

Sep 2, 2020 at 3:36pm
Hi,

Is the NULL pointer same as an uninitialized pointer?

Thanks
Sep 2, 2020 at 3:59pm
No.
Sep 2, 2020 at 4:01pm
Could you give me an example?

Is it NULL pointer?

int *p = NULL;
and is it uninitialized pointer?

int *p;
Sep 2, 2020 at 4:02pm
An uninitialised pointer can have any value - whatever value happens to be in memory at which it is defined. The NULL (or nullptr in C++) is a pointer initialised to a known value. It's the same as having an uninitialised variable.
Sep 2, 2020 at 5:48pm
some compilers zero uninitialized memory in debug mode. If yours does this and you are in debug mode, it may seem they are the same, but you must not rely on this.
side note, most coders consider it good practice to initialize everything. It isn't necessary for c++, but it helps avoid obscure bugs and helps when debugging to see things go wrong.
Sep 3, 2020 at 7:50am
Is it NULL pointer?

int *p = NULL;


Yes - but in C++ it should be

int *p = nullptr;

and is it uninitialized pointer?

int *p;


Yes. The value of p could be anything.
Topic archived. No new replies allowed.