Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

C's NULL sucks, because it's not a unique value and thus can be "lost" or confused with other values.

Having a distinct null which can only be arrived at by typing the characters "null" is a good thing, imo.



Can you tell a story about how NULL bit you in the ass in real code?


I remember one early version of a C++ compiler which had NULL #defined as (( void* )0), which caused all manner of fun trying to compile some legacy C code.

This was in 1999, I think.


You mean like this?

#define NULL __DARWIN_NULL

...

#define __DARWIN_NULL ((void *)0)


Something like that. It makes the following invalid in C++ but valid in C:

  int *ptr = NULL;


Except for the fact that the real definition is actually more like:

    #ifdef __cplusplus
    #define __DARWIN_NULL __null
    #else /* ! __cplusplus */
    #define __DARWIN_NULL ((void *)0)
    #endif /* __cplusplus */
So the code you mentioned works just fine in C++.


Except that the actual compiler I was using was for the PlayStation 2, so I very much doubt it had that precise code you just outlined, and it really did have NULL defined as ((void* )0), and it really did break. I saw it happen.


Isn't the right solution to make C NULLs a unique value or at least get rid of them now that a better replacement is here. Seriously, have 5 different ways of doing the same thing is how perl became a hell hole for anything more than 10 lines.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: