Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is NULL defined as nullptr in C++11?

Will C++11 implementations define NULLas nullptr?

Would this be prescribed by the new C++ standard?

like image 299
Martin Ba Avatar asked Aug 26 '11 06:08

Martin Ba


People also ask

IS null equivalent to nullptr?

nullptr is meant as a replacement to NULL . nullptr provides a typesafe pointer value representing an empty (null) pointer. The general rule of thumb that I recommend is that you should start using nullptr whenever you would have used NULL in the past.

What is nullptr in C++11?

The C++11 standard introduced a new keyword, nullptr as a null pointer constant. The nullptr constant can be distinguished from integer 0 for overloaded functions.

Where is nullptr defined in C?

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

Can we define null in C?

In computer programming, null is both a value and a pointer. Null is a built-in constant that has a value of zero. It is the same as the character 0 used to terminate strings in C. Null can also be the value of a pointer, which is the same as zero unless the CPU supports a special bit pattern for a null pointer.


1 Answers

From the horse's mouth

C.3.2.4 Macro NULL [diff.null]

1/ The macro NULL, defined in any of <clocale>, <cstddef>, <cstdio>, <cstdlib>, <cstring>, <ctime>, or <cwchar>, is an implementation-defined C++ null pointer constant in this International Standard (18.2).

It is up to each implementation to provide its own definition, gcc if I recall correctly defines it to __nullptr for which it has special checks (verifies that it is not used in arithmetic contexts for example).

So it is possible to define it as nullptr, you will have to check your compiler/Standard Library documentation to see what has been done.

like image 60
Matthieu M. Avatar answered Sep 30 '22 20:09

Matthieu M.