[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: NULL is not NUL
- From: sur-behoffski <sur_behoffski@...>
- Date: Thu, 12 Nov 2020 08:37:26 +1030
I see people getting conflating the names, and then getting confused
by, two different, but similar-seeming things:
1. The character NUL ('\0'); versus
2. The NULL pointer ((void *) 0).
By definition, sizeof(char) == 1. This is even if the underlying architecture
(e.g. some DSP platforms) actually has 32-bit characters. When potential
confusion arises because of the width of "char", standards often use the term
"octet" to refer to an 8-bit group.
The name NUL comes from ASCII.
Using memset to set some character(s) to all-bits-0 will set all characters
C defines any pointer that reads as 0, or is written to as 0, as a NULL
pointer, guaranteed to be different to any valid pointer. As others have
said, the OS/runtime/compiler may choose any value "under the surface",
but it will always read and write as 0 when used in a standards-compliant
fashion at the abstract C code level.
1. Never use "null" -- it's too vague;
2. If talking strictly about characters, use NUL and/or NULs;
3. If talking strictly about pointers, use NULL and/or NULLs.
4. memset can set a memory area (e.g. sizeof(struct thingy) to
"all-bits-0". This may make the struct (more deterministic to
reason about. However, after memset, all pointers in the struct
should be explicitly set to NULL.
Hope this helps,
sur-behoffski (Brenton Hoff)
programmer, Grouse Software