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
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
  • 603
  • 5
  • 12
28
votes
5 answers

c++ access static members using null pointer

Recently tried the following program and it compiles, runs fine and produces expected output instead of any runtime error. #include class demo { public: static void fun() { std::cout<<"fun() is called\n"; …
Destructor
  • 14,123
  • 11
  • 61
  • 126
27
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
  • 14,072
  • 2
  • 31
  • 53
25
votes
3 answers

Is nullptr falsy?

When used as a boolean expression or transformed into a boolean either explicitly or implicitly, is nullptr consistently false? Is this implementation defined or specified in the standard? I wrote some code to test, but am not certain if it tests…
24
votes
4 answers

What header file needs to be included for using nullptr in g++?

I am using g++ 4.4.1 and want to use nullptr, but I am not being able to find which header file is required to be included. It does not seem to be keyword either, because my attempt to use it is rejected as error: 'nullptr' was not declared in this…
Arun
  • 19,750
  • 10
  • 51
  • 60
23
votes
1 answer

Why `void* = 0` and `void* = nullptr` makes the difference?

I was playing with SFINAE and found behavior I cannot explain. This compiles fine: template::value>* = nullptr> void foo(Integer) {} template
Mikhail
  • 20,685
  • 7
  • 70
  • 146
23
votes
1 answer

When NULL cannot be replaced by nullptr?

I am refactoring some older code that uses NULL in many places. The question is Is it safe to blindly replace all NULL instances by nullptr? I am particularly interested in scenario where replacing NULL by nullptr may lead to some run-time errors…
Ilya Kobelevskiy
  • 5,245
  • 4
  • 24
  • 41
21
votes
2 answers

How does c++ nullptr implementation work?

I am curious to know how nullptr works. Standards N4659 and N4849 say: it has to have type std::nullptr_t; you cannot take its address; it can be directly converted to a pointer and pointer to member; sizeof(std::nullptr_t) == sizeof(void*); its…
Fullfungo
  • 345
  • 2
  • 13
21
votes
4 answers

Can I check a C++ iterator against null?

I'm having trouble with vector iterators. I've read in a few places that checking for null iterators isn't possible, and that the usual way to check iterators is to check it against vector.end() after a search. So for example: vector< Animal* >…
Zebrafish
  • 11,682
  • 3
  • 43
  • 119
21
votes
7 answers

What is the difference between nullptr and nullptr_t in C++?

Which one should I use? Any advantages if I use one over the other?
Moiz Sajid
  • 644
  • 1
  • 10
  • 20
21
votes
2 answers

Should one use a std::move on a nullptr assignment?

I came across the following. Is there any advantage to doing a move on the nullptr? I assume it is basically assigning a zero to Node* so I am not sure if there is any advantage to do a move here. Any thoughts? template struct Node { …
bjackfly
  • 3,236
  • 2
  • 25
  • 38
20
votes
2 answers

Can nullptr be used as a variable argument (varargs)?

Can I use the nullptr keyword as an argument for a variable function? If so, does it undergo any kind of standard conversion, and what is the type of the resulting value? Concretely, is the following correct? std::printf("%p", nullptr); Or does it…
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
19
votes
4 answers

Is NULL defined as nullptr in C++11?

Will C++11 implementations define NULLas nullptr? Would this be prescribed by the new C++ standard?
Martin Ba
  • 37,187
  • 33
  • 183
  • 337
19
votes
1 answer

Is it legal to call delete on a null pointer of an incomplete type?

And if so, why does the following code give me the warning note: neither the destructor nor the class-specific operator delete will be called, even if they are declared when the class is defined ? struct C; int main() { C *c = nullptr; …
Dan M.
  • 3,818
  • 1
  • 23
  • 41
19
votes
3 answers

Are these null pointers, or are they pointers to address 0?

If I write int zero = 0; void *p1 = (void *)0; void *p2 = (void *)(int)0; void *p3 = (void *)(0 /*no-op, but does it affect the next zero?*/, 0); void *p4 = (void *)zero; // For reference, this is a pointer to address zero void *p5 = 0; …
user541686
  • 205,094
  • 128
  • 528
  • 886
1
2
3
23 24