Why is address zero used for null pointer?
Posted
by Joel
on Stack Overflow
See other posts from Stack Overflow
or by Joel
Published on 2010-05-03T17:16:56Z
Indexed on
2010/05/03
17:28 UTC
Read the original article
Hit count: 195
In C (or C++ for that matter), pointers are special if they have the value zero: I am adviced to set pointers to zero after freeing their memory, because it means freeing the pointer again isn't dangerous; when I call malloc it returns a pointer with the value zero if it can't get me memory; I use if (p != 0)
all the time to make sure passed pointers are valid etc.
But since memory addressing starts at 0, isn't 0 just as a valid address as any other? How can 0 be used for handling null pointers if that is the case? Why isn't a negative number null instead?
© Stack Overflow or respective owner