Questions tagged [nullptr]

The C++11 keyword for a null pointer, it can be converted to any pointer type. Also available in C23 through stddef.h. Always use this tag either in combination with the C++ or the C tag.

In C++, nullptr is a null pointer constant of type std::nullptr_t which can be implicitly converted to any pointer type. An integer constant 0 can also be converted to any pointer type but can also be deduced as an integer in other contexts, leading to ambiguities. See What exactly is nullptr?

In C ("C23" ISO 9899:202x), nullptr is similarly a null pointer constant of type nullptr_t found in stddef.h. It can get converted to a void*, bool or a pointer type.

349 questions
655
votes
14 answers

What exactly is nullptr?

We now have C++11 with many new features. An interesting and confusing one (at least for me) is the new nullptr. Well, no need anymore for the nasty macro NULL. int* x = nullptr; myclass* obj = nullptr; Still, I am not getting how nullptr works.…
Khaled Alshaya
  • 94,250
  • 39
  • 176
  • 234
330
votes
4 answers

NULL vs nullptr (Why was it replaced?)

I know that in C++ 0x or NULL was replaced by nullptr in pointer-based applications. I'm just curious of the exact reason why they made this replacement? In what scenario is using nullptr over NULL beneficial when dealing with pointers?
Riptyde4
  • 5,134
  • 8
  • 30
  • 57
174
votes
7 answers

What are the advantages of using nullptr?

This piece of code conceptually does the same thing for the three pointers (safe pointer initialization): int* p1 = nullptr; int* p2 = NULL; int* p3 = 0; And so, what are the advantages of assigning pointers nullptr over assigning them the values…
Mark Garcia
  • 17,424
  • 4
  • 58
  • 94
96
votes
4 answers

C++11 When clearing shared_ptr, should I use reset or set to nullptr?

I have a question about C++11 best practices. When clearing a shared_ptr, should I use the reset() function with no parameter, or should I set the shared_ptr to nullptr? For example: std::shared_ptr foo(new…
user1930581
  • 1,465
  • 1
  • 13
  • 23
74
votes
7 answers

Why not call nullptr NULL?

In C++11 the nullptr keyword was added as a more type safe null pointer constant, since the previous common definition of NULL as 0 has some problems. Why did the standards committee choose not to call the new null pointer constant NULL, or declare…
user253751
  • 57,427
  • 7
  • 48
  • 90
48
votes
5 answers

Can nullptr be emulated in gcc?

I saw that nullptr was implemented in Visual Studio 2010. I like the concept and want to start using it as soon as possible; however GCC does not support it yet. My code needs to run on both (but doesn't have to compile with other compilers). Is…
nuzz
  • 481
  • 1
  • 4
  • 3
44
votes
4 answers

Why can't you take the address of nullptr?

In the C++11 standard, I don't understand the reason why taking the address of nullptr is disallowed whereas one is allowed to take the address of their own std::nullptr_t instances. Aside from the fact that nullptr is a reserved keyword, is there…
monkey0506
  • 2,489
  • 1
  • 21
  • 27
43
votes
4 answers

Can the NULL macro actually be a nullptr?

According to the draft of the standard N4713 (7.11/1): A null pointer constant is an integer literal (5.13.2) with value zero or a prvalue of type std::nullptr_t. and 21.2.3/2: The macro NULL is an implementation-defined null pointer…
αλεχολυτ
  • 4,792
  • 1
  • 35
  • 71
42
votes
6 answers

Is it safe to #define NULL nullptr?

I have seen below macro in many topmost header files: #define NULL 0 // C++03 In all over the code, NULL and 0 are used interchangeably. If I change it to. #define NULL nullptr // C++11 Will it cause any bad side effect ? I can think of the only…
iammilind
  • 68,093
  • 33
  • 169
  • 336
41
votes
5 answers

Why is C++'s NULL typically an integer literal rather than a pointer like in C?

I've been writing C++ for many years, using nullptr for null pointers. I also know C, whence NULL originates, and remember that it's the constant for a null pointer, with type void *. For reasons, I've had to use NULL in my C++ code for something.…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
41
votes
8 answers

Pure virtual functions in C++11

In C++98, the null pointer was represented by the literal 0 (or in fact any constant expression whose value was zero). In C++11, we prefer nullptr instead. But this doesn't work for pure virtual functions: struct X { virtual void foo() =…
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
36
votes
2 answers

Why isn't 'nullptr' in the 'std' namespace?

It seems that nullptr is declared in the default global namespace. Wouldn't it make sense for it to be in the std namespace?
MWB
  • 11,740
  • 6
  • 46
  • 91
36
votes
4 answers

What are the uses of the type `std::nullptr_t`?

I learned that nullptr, in addition to being convertible to any pointer type (but not to any integral type) also has its own type std::nullptr_t. So it is possible to have a method overload that accepts std::nullptr_t. Exactly why is such an…
Hindol
  • 2,924
  • 2
  • 28
  • 41
31
votes
3 answers

Why does const auto &p{nullptr} work while auto *p{nullptr} doesn't in C++17?

This definition works: const auto &b{nullptr}; while this fails: auto *b{nullptr}; I have tried to compile this in Visual C++, GCC, and Clang. They all complain "cannot deduce type". In the second case, shouldn't b be deduced to have some type…
felix
  • 2,213
  • 7
  • 16
1
2 3
23 24