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.
Printable View
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++.
> Isn't NULL just the value 0, just like in C?
It should be.
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
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.
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).
>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