btaylor2401
Pointers in C do not *act* like integers. Integer math does not apply directly to pointer computation, confusing integer and pointer types is a really good way to create all sort of nasty, nasty bugs:
1 2 3 4 5 6 7 8 9 int i = 0; char * p1 = 0; uint16_t * p2 = 0; uint32_t * p4 = 0; ++i; // i = 1 ++p1; // p1 = 1 ++p2; // p2 = 2 ++p4; // p4 = 4
This difference in operation in itself prevents bugs (because you can actually use pointer math effectively without including sizeof() everywhere.) And the fact that pointers hold integer values *is* useful information, and impacts your use of them. But the types should not be conflated, because the operations are different.
C is not assembly. It is designed to do some semantic abstraction of the CPUs operation.
ratchetfreak
I just have an issue with the fact that "void* ptr = 0;" was ever valid code.
cmuratori
But why? Do you also have an issue with saying "long long value = 0;" instead of, say, "long long value = 0LL;"? Or how about "float value = 0;" instead of "float value = 0.0f;"?
- Casey
1 2 | void foo( int x ); void foo( void* x ); |
ratchetfreak
An numeric value is not a pointer if you want to convert a numeric value into a pointer you should be explicitly casting in all cases.
cmuratori
I really just have a hard time seeing these sorts of things as anything but silly. As an external observer, the idea that "you have to always cast 0 to a void * in order to specifically call it a pointer" is somehow this big motivating factor in why you would want nullptr is really strange. Are we just talking about the fact that (void*)0 is one more character to type than nullptr? I mean do people really think that was a good reason to add yet another concept to an already bloated language?
I would argue that (void *)0 is _much_ better than nullptr, because _at least it's obvious what it is_. Case in point, something like this thread is never going to happen if you just always have (void *)0, because it's obvious that it's just the value 0 being specifically made into a pointer type. Contrast that with NULL or nullptr, which _nobody_ knows what they mean unless you've gone and read some additional documentation that discusses it. They're totally ancillary and additional concepts, which happen to also be unnecessary.
Languages should add features that _actually do something useful_. Things like NULL and nullptr are the kinds of things that drive me nuts, because they don't actually do anything, and they just end up obscuring what the program is doing for no apparent benefit, and adding more things for beginners to learn that don't end up allowing them to write more powerful programs.
It's busywork - for the language designers, the language implementors, and the programmers who use the language :(
- Casey
But why? Do you also have an issue with saying "long long value = 0;" instead of, say, "long long value = 0LL;"? Or how about "float value = 0;" instead of "float value = 0.0f;"?Well, if you have a function that is overloaded to take either a int or a pointer, the strong typing of nullptr is nice, same applies to long long and double/float. Using nullptr also makes it clear that I am talking about a pointer value (that happens to be 0), not just some integer. Allowing for coercion in the language can makes this less clear, similar to how it is not obvious from the call site that a function may be taking a c++ reference.
- Casey
1 2 3 | if (ptr) { foo(ptr); } |
1 2 3 | if (ptr != NULL) { foo(ptr); } |
poe
Bit of a bump, but found this thread on Google. For me personally, I have found just using 0 as the null pointer to be wonderful. Have had zero bugs resulting from this practice (though I work alone, so don't have to worry about "best practices"), and probably saved myself a bunch of typing and mental overhead. Zero is initialization is the new mantra. Thanks Casey!
I like how this understanding makes working with pointers more succinct:
1 2 3 if (ptr) { foo(ptr); }
as opposed to
1 2 3 if (ptr != NULL) { foo(ptr); }
Same deal with the string "null terminator," i.e. 0.
In summary: Having a more unified understanding + intuition about 0 has been really nice.