I read on this other forum a member saying that NULL does not exist in C++ and that one might get a compiler error using it. Is it true? Isn't NULL just the value 0, just like in C? Thanks.
I read on this other forum a member saying that NULL does not exist in C++ and that one might get a compiler error using it. Is it true? Isn't NULL just the value 0, just like in C? Thanks.
NULL is not a built-in type, but is a macro and hence can be defined as anything. I believe that the standard does require that when pointers are assigned NULL that NULL translate to a guarenteed invaild pointer (on most machines that would in fact be zero, but not on all machines). NULL is also a macro in C, so you can't guarenteed that it will behave either, though I suspect the standard calls for the same or similiar results as for C++.
Free code: http://sol-biotech.com/code/.
It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
--Me, I just made it up
The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
--George Bernard Shaw
> Isn't NULL just the value 0, just like in C?
It should be.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
I think NULL isn't C++.
One has to include some headerfile which defines it....
Any way I'm not sure...
NULL is a value (often a macro) defined in some standard C headers (eg stdlib.h), and is available in C++ if you #include those headers. If you don't #include a header that defines NULL, a compiler error will result (in both C and C++) if you use it.
NULL is not required to be the value zero, in either C or C++. What is required is that the assignment "x = 0;", where x is a pointer to Type, is required to have the same effect as "x = (Type *)NULL". In practice, that often means that NULL has a value zero, but (as mitakeet says) there are some machines where that is not true.
Note: the above is definitely true of the 1989 C standard and the C++ standard. I haven't checked if it is true of the 1999 C standard.
I thought NULL was ((void*)0)
taken from malloc.h
Code:#if defined(__STDC__) || defined (__cplusplus) # include <stddef.h> # define __malloc_ptr_t void * #else # undef size_t # define size_t unsigned int # undef ptrdiff_t # define ptrdiff_t int # define __malloc_ptr_t char * #endif #ifndef NULL # ifdef __cplusplus # define NULL 0 # else # define NULL ((__malloc_ptr_t) 0) # endif #endif
signature under construction
As mentioned before, NULL is OS and compiler dependant, so showing a section of malloc.h without specifying that information is useless.
On MOST hardware/OS/compiler combinations that MOST of the people browsing this forum are going to encounter zero is the expected value for NULL. When it isn't zero for your hardware/OS/compiler using zero instead of the macro will get you into a world of hurt.
Free code: http://sol-biotech.com/code/.
It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
--Me, I just made it up
The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
--George Bernard Shaw
well i just meant:
in these headers NULL is defined explicitely for c++
also, when you look at c++ headers, you see that they also use NULL
edit:
so independed of wheter youre using c or cpp, both use pointers (where c++ references are pointers that behave syntactically as if they were the object itself)
so whenever you have a pointer there must be a NULL value
(of course there would be the option:
but that would waste one byte per pointer.Code:struct Pointer { bool is_p_pointing_to_a_valid_location; void *p; };
thus its more useful of reserving ONE byte at some address to be the NULL address (e.g. 0x00000000. of course it could also be 0x12345678 or 0xffff).
Last edited by Raven Arkadon; 06-29-2005 at 09:05 AM.
signature under construction
>When it isn't zero for your hardware/OS/compiler using zero instead of the macro will get you into a world of hurt.
No, it really won't: http://www.research.att.com/~bs/bs_faq2.html#null