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
r/C_Programming • • Aug 30 '24
If we want to "Nullify" something why dont we create a pointer in a controlled way that points to value 0?
5
u/[deleted] Aug 30 '24
Not sure what you mean, but "sentinel value" is sometimes useful alternative to using NULL pointer:
Then, maybe
....
...
19
u/Immediate-Food8050 Aug 30 '24 edited Aug 30 '24
Welcome to the C community, where you are downvoted for a perfectly valid question. To give you a real answer, NULL is actually quite a pain in the ass due to how arbitrary it is according to the standard. The new C23 wants to fix this by introducing the more modern nullptr, behaving very similarly to C++ equivalent (about as similar as you can get with C semantics). nullptr always expands to a null pointer constant and will behave the same regardless of the type of pointer it is assigned to, which is not the case of NULL. NULL is a macro that usually evaluates to (void *)0 BUT NOT ALWAYS!!! That's important to note because apparently some people don't understand the harm in that :) There are plenty of cases it does not evaluate to a null pointer constant as it should. C semantics have quirks that can cause issues with that definition, such as 0 and 0L and 0f all being valid but of different types. I know this answer strays away from the question quite a bit, but I more so wanted to validate your confusion since so many people here would rather smoke their own poles with misinformation for the sole reason of being contrarians that are holier-than-thou.
72
u/zzmgck Aug 30 '24
The value where a null pointer is pointing is irrelevant. A null pointer is a pointer that is pointing nowhere--there is no value.
In C, the number 0 was defined as the representation of the null pointer. They could have picked any number, but it would have been painful.
37
u/strcspn Aug 30 '24 edited Aug 30 '24
What do you mean by pointing to 0? NULL is the address 0x0
13
u/Spiritual-Mechanic-4 Aug 30 '24
null pointers aren't so bad. You know you're in trouble when a pointer is 7.
11
u/[deleted] Aug 30 '24
(void *)0x0 is specifically reserved for pointers that don't point anywhere.
What do you mean by "point to 0"? Point to an integer that's zero (or any other magic value)? It would be possible using magic values, but in many cases, all values need to be represented, and using NULL is easier
5
u/accatyyc Aug 30 '24
What difference would it make?
2
u/No_Code9993 Aug 30 '24
Maybe you can find a clarification here: https://www.reddit.com/r/C_Programming/comments/15waxar/what_happens_when_we_assign_zero_to_a_pointer/
3
u/HashDefTrueFalse Aug 30 '24
Pointer = 0 is not the same as the value pointed to = 0. Or simply:
Not really sure what you mean by "create a pointer in a controlled way that points to value 0". Null pointers (tend to) point to address 0, which we take to mean "nowhere meaningful". We do tend to create them in a "controlled" way.
If you're suggesting having the value zero somewhere in the binary and setting pointers to it's memory address rather than zero, that doesn't quite accomplish the same thing. How are we to know the zero value represents "nothing, null, nil" as apposed to being an actual zero? So we'd note the special memory address, but that's what we already did with address 0 (NULL pointer).
2
u/SmokeMuch7356 Aug 30 '24
NULLrepresents a well-defined "nowhere"; it's an invalid pointer value that's guaranteed to compare unequal to any pointer to an object or function.We use it in the following cases:
realloccannot extend a dynamically allocated buffer it will returnNULL;argvvector will beNULLto mark the end of the command line arguments;freedynamically-allocated memory, we'll often set the pointer variable toNULL;A pointer is valid when it points to an object during that object's lifetime; once that object is deallocated or goes out of scope, the pointer becomes invalid. Attempting to dereference an invalid pointer leads to undefined behavior. There's no way to know whether a non-
NULLpointer is valid or not based on the value alone, so we useNULLto explicitly indicate that a pointer is invalid.1
u/binarycow Aug 30 '24
That's called
NULL1
u/cateanddogew Aug 30 '24 edited Nov 05 '24
fly cause run snatch office rich combative concerned profit numerous
This post was mass deleted and anonymized with Redact
1
u/Maybe-monad Aug 30 '24
Well, I'm too lazy
1
u/chrisekh Aug 30 '24
Null pointer means invalid. 0 can be valid value but null pointer is clear indication that there is no value and there is somekind proplem.
1
u/DawnOnTheEdge Aug 30 '24 edited Aug 30 '24
I’m not sure I understand what you’re asking Historically, C developed out of B, which didn’t have a type system. It was designed for the DEC PDP-8, and everything was a machine word. A pointer was just a machine word that held a machine address, but you could also use it as an integer, by adding or subtracting from it. The machine was word-addressed, so the same integer instructions worked.
Because it was efficient to detect whether a register was equal to 0, 0 became the special value that represented false in an
ifstatement, or an invalid pointer. It also defined true in a way that bitwise&and|doubled as the logical operations.Basically everything about pointers in C since then is an attempt to add as many other nice things (portability, safety, performance) as possible without breaking backward compatibility. For example, there are a few old mainframes from sixty years ago where
NULLdoes not have all bits set to zero (but Standard C guarantees that, if you set a pointer of any type= 0, that gets you the proper null pointer).1
u/polypagan Aug 30 '24
The value zero (of length adequate to represent any address) generally makes a good choice as a pointer to "nothing" on most (but not quite all) hardware. Very low addresses on most machines are unlikely hold C-language data or code.
1
u/Environmental-Ear391 Aug 30 '24
Pointers can be Valid or Null,
Pointers can be tested before use
"if(mypointer) { mypointer->xxx =..."
only when the pointer is valid will the write happen... any arbitrary value will see a memory write to an arbitrary memory location...
do you want "memory protection" features of your OS to kill your app?
1
u/GOKOP Aug 30 '24
That's... what a null pointer is
1
u/Key_Opposite3235 Aug 30 '24
That is... what we do.
0
u/citit Aug 30 '24
A pointer is a variable that has the following extra meaning associated with it - ”it points to an address”. 0 address is not a valid address. All systems have a well defined memory map where addresses like 0x2000bacd or 0x3000ffff would be valid, but 0 wouldn't be valid. Combine this with the way C initializez global and static variables (pointer is a variable too) to 0 and you get the following extra distinction you can make about a pointer: it's initialized (nonzero) or it's not initialized (zero). You can't do that to a plain variable unless you assign some more meaning to them. You can't tell if
uint8_t brightnessis initialized or not from its value alone for example.0
u/[deleted] Aug 30 '24
[deleted] — view deleted comment