[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: NULL is not NUL
- From: Will Crowder <will@...>
- Date: Wed, 11 Nov 2020 18:32:03 -0800
EVERYONE's mileage may vary on this, but because "0" is the null pointer constant, and because the standard doesn't have any useful definition of NULL (it can be "0" or "(void *)0"; the latter would be useful if it were the mandated standard, but it's not), I tend to simply use "0" directly when dealing with NULL pointers.
I realize I'm probably in the minority on this (and I always use NULL if the surrounding code is), but it's worked for me over my many years of C programming and generally (again, just IMHO) it's a bit less cluttered.
(And yes, I cringe when I see NUL where NULL is supposed to be, and vice versa...)
On Thu, 2020-11-12 at 08:37 +1030, sur-behoffski wrote:
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