Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

Meaning and Definition of NULL

Programming languages, Coding, Executables, Package Creation, and Scripting.
Post Reply
Message
Author
bentHnau
Posts: 148
Joined: 2014-01-07 01:43
Been thanked: 1 time

Meaning and Definition of NULL

#1 Post by bentHnau »

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?

peter_irich
Posts: 1403
Joined: 2009-09-10 20:15
Location: Saint-Petersburg, Russian Federation
Been thanked: 11 times

Re: Meaning and Definition of NULL

#2 Post by peter_irich »

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.

User avatar
ruwolf
Posts: 623
Joined: 2008-02-18 05:04
Location: Banovce nad Bebravou
Has thanked: 35 times
Been thanked: 26 times

Re: Meaning and Definition of NULL

#3 Post by ruwolf »

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.

neuraleskimo
Posts: 195
Joined: 2019-03-12 23:26

Re: Meaning and Definition of NULL

#4 Post by neuraleskimo »

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...

Post Reply