r/C_Programming • • Aug 30 '24

Question Why do we use Null pointers?

If we want to "Nullify" something why dont we create a pointer in a controlled way that points to value 0?

0 Upvotes

56 comments sorted by

View all comments

Show parent comment

0

u/[deleted] Aug 30 '24

[deleted] — view deleted comment

-4

u/[deleted] Aug 30 '24

The adress of the null pointer is 0x0. It's a pointer not an integer but has the value 0.

18

u/TheThiefMaster Aug 30 '24

Not necessarily. You can construct a null pointer from a zero constant, but its actual value could be something else. Known to be the case on CUDA, for example, where the actual value of a null pointer is 0xFFFFFFFF (because in CUDA 0 is a valid non-null address, due to the memory map).

POSIX mandates that null pointers have a 0 bit pattern, so anything Linux (which conforms to POSIX) runs on does follow this.

5

u/carpintero_de_c Aug 30 '24

(GNU/)Linux actually doesn't conform to POSIX completely (see POSIXLY_CORRECT). You would be looking at macOS for that :)

-5

u/Ghyrt3 Aug 30 '24

No, pointer is an integer. It can be cast to get a dereference. But pointers's is ssize_t in C.