Questions tagged [nullptr]

The C++11 keyword for a null pointer, it can be converted to any pointer type.

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?

305 questions
598
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.…
AraK
  • 87,541
  • 35
  • 171
  • 230
252
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
  • 4,214
  • 7
  • 25
  • 51
167
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
  • 16,438
  • 3
  • 50
  • 93
74
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,235
  • 1
  • 11
  • 22
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
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
  • 62,239
  • 27
  • 150
  • 297
42
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,408
  • 1
  • 23
  • 62
42
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,236
  • 1
  • 19
  • 25
40
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
  • 237,063
  • 85
  • 359
  • 638
35
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?
bobcat
  • 9,713
  • 6
  • 34
  • 72
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
  • 1,919
  • 5
  • 15
30
votes
2 answers

Does the standard behavior for deleters differ between shared_ptr and unique_ptr in the case of null pointers?

OK, so first some things that might be relevant: I'm using the Clang 3.1 compiler, in C++11 mode, with the standard library set to libc++. I'm trying to familiarize myself with C++11, and in so doing I ran across behavior that seems odd. It may be a…
Bob Miller
  • 572
  • 3
  • 11
27
votes
3 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,661
  • 1
  • 23
  • 39
26
votes
3 answers

Can you compare nullptr to other pointers for order? Is it always smaller?

This question is based on code that I found that monitors possible memory leaks, so it contains some code that you probably don't want to see in regular programs like ordering pointers. However, I saw that a pointer was set to nullptr and then the…
stefaanv
  • 12,981
  • 2
  • 27
  • 48
1
2 3
20 21