Meaning and Definition of NULL

Need help with C, C++, perl, python, etc?

Meaning and Definition of NULL

Postby bentHnau » 2018-12-29 04:41

When I look in stddef.h. I see that NULL has multiple definitions:

Code: Select all
403 #define NULL __null
404 #else   /* G++ */
405 #ifndef __cplusplus
406 #define NULL ((void *)0)
407 #else   /* C++ */
408 #define NULL 0

How is the appropriate definition for a given context chosen? And what exactly does ((void *) 0) mean? My book says that it is simply an address, but what does the 0 represent?
Debian Stretch x64 on a Dell XPS 17 laptop
bentHnau
 
Posts: 125
Joined: 2014-01-07 01:43
Location: California

Re: Meaning and Definition of NULL

Postby peter_irich » 2018-12-29 12:55

I think it is simply data type (void *). There are values of the different types: int, double, float and others
and pointers to this types, its differs by length and format, so there is and type (void *).
"0" means the this pointer does not dereference.

Peter.
peter_irich
 
Posts: 1254
Joined: 2009-09-10 20:15
Location: Saint-Petersburg, Russian Federation

Re: Meaning and Definition of NULL

Postby ruwolf » 2019-01-28 01:42

It depends on version of compiler.
If you run it as C++, definition is simple:
0
If you run it as C (which is not C++, #ifndef is command of preprocessor meaning "if not defined"), it is pointer to 0. It is used as type void pointer, because this type of pointer can be in C converted to pointer to object of different type without explicit cast, e.g. implicitly.
Macro __cplusplus is defined, if compiler is running as C++; if compiler is runing as C, it is not defined.
User avatar
ruwolf
 
Posts: 392
Joined: 2008-02-18 05:04
Location: Slovakia, Banovce nad Bebravou, Matice slovenskej 1260/4-7

Re: Meaning and Definition of NULL

Postby neuraleskimo » 2019-03-13 00:25

Good questions...

How is the appropriate definition for a given context chosen?

__cplusplus, etc. are macros that are defined elsewhere based on the specific environment in which the compiler was built or the code being compiled is built (in the case of __cplusplus).

And what exactly does ((void *) 0) mean?

void* is a type. The type of a pointer with its type cast away to be more specific. You have probably seen char* or int*. These are pointers to characters or integers respectively. In some instances, it is useful to cast away the specific type or return an address to memory without a type. For example, malloc returns a chunk of bytes (void*) and you give it the type (e.g., int* p = malloc(sizeof(int) * 1024)).

My book says that it is simply an address, but what does the 0 represent?

Nothing. Seriously, it is a memory address that is never allocated, so it represents a pointer to nowhere. It is similar to the null terminator of a string.

Hopefully this helps...
User avatar
neuraleskimo
 
Posts: 102
Joined: 2019-03-12 23:26
Location: Bloomington, Indiana, USA


Return to Programming

Who is online

Users browsing this forum: No registered users and 1 guest

fashionable