is it necessary to call pointer = NULL when initializing?

user1051003 picture user1051003 · Sep 3, 2012 · Viewed 23.4k times · Source

when I create a pointer to certain struct, do I have to set it to NULL, then alloc it then use it? and why?

Answer

Levon picture Levon · Sep 3, 2012

No, you don't have to set it to NULL, but some consider it good practice as it gives a new pointer a value that makes it explicit it's not pointing at anything (yet).

If you are creating a pointer and then immediately assigning another value to it, then there's really not much value in setting it to NULL.

It is a good idea to set a pointer to NULL after you free the memory it was pointing to, though.