What header defines NULL in C++?

C++Null

C++ Problem Overview


According to C++ primer, <cstdlib> header defines NULL. cpluspplus says it is defined in <cstddef>.

Ultimately, if the right header is not included, I thought NULL can't be referenced.

From what i can see, however it can be referenced and produce programs and that compile and run without warnings or errors, after including only <iostream>

Please help me understand this.

C++ Solutions


Solution 1 - C++

The C standard requires that NULL be defined in locale.h, stddef.h, stdio.h, stdlib.h, string.h, time.h, and wchar.h.

The C++ standard requires that NULL be defined in the c* header corresponding to each of those.

The C standard is very strict about the names a standard can define--each standard header must define precisely the names the standard requires that header to define. The only other names it can define are those that are reserved for the implementation, such as those starting with an underscore followed by another underscore or a capital letter.

The C++ standard is much more permissive in this respect--including any one standard header can have the same effect as including any or all other standard headers.

From a practical viewpoint, C++ implementations used to take quite a bit of advantage of this permissiveness--that is, including one standard header frequently defined the names from a number of other standard headers. More recent implementations tend to work more like the C standard requires, staying much closer to each header defining only the names required by to be defined by that header. They're still probably not as strict about it as the C standard requires, but much closer than they used to be (as a rule).

Solution 2 - C++

C++03 section 18.1.2 says that NULL is defined in cstddef.

On some implementations, iostream may include cstddef, so including iostream would also give you NULL.

Solution 3 - C++

It is defined in <cstddef>

The C++11 standard section 18.2, table 30 explains what's in <cstddef> It says:

> Table 30 — Header <cstddef> synopsis > > Macros: NULL [...] > > [...] The macro NULL is an implementation-defined C++ null pointer constant in this International Standard

Solution 4 - C++

The C++11 Standard says NULL must be defined in multiple files. They are:

<clocale>
<cstddef>
<cstdlib>
<cstring>
<ctime>
<cwchar>

This is mentioned in Table 149, Section C.3 C standard library/3 of the standard.

Here's an image of the table and some surrounding text.

enter image description here

Solution 5 - C++

In the latest version of MinGW (MinGW 5.3.0) NULL was defined in the wchar.h header as :

#define NULL 0

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJames LeonardView Question on Stackoverflow
Solution 1 - C++Jerry CoffinView Answer on Stackoverflow
Solution 2 - C++Vaughn CatoView Answer on Stackoverflow
Solution 3 - C++CornstalksView Answer on Stackoverflow
Solution 4 - C++R SahuView Answer on Stackoverflow
Solution 5 - C++Arshia AghaeiView Answer on Stackoverflow