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
8
votes
2 answers

How is nullptr rvalue

While looking at the implementation of nullptr here, what got my attention is that nullptr is rvalue which means we can do something like this std::nullptr_t&& nullref = nullptr; But how could nullptr be rvalue since the implementations is…
Laith
  • 1,248
  • 2
  • 11
  • 19
8
votes
4 answers

nullptr as a template parameter

I have a template like: template void func(A* a, B* b){ ... } In some cases it happens that parameter B* b is not needed and therefore, I try to use a nullptr: MyA a; func(&a, nullptr); The compiler doesn't like that since…
Michael
  • 7,407
  • 8
  • 41
  • 84
7
votes
2 answers

If make_shared/make_unique can throw bad_alloc, why is it not a common practice to have a try catch block for it?

The CppReference page for make_shared says (same with make_unique) May throw std::bad_alloc or any exception thrown by the constructor of T. If an exception is thrown, the functions have no effect. This means that std::bad_alloc exeception can…
Boanerges
  • 476
  • 1
  • 5
  • 16
7
votes
1 answer

Expected unqualified-id before ‘nullptr’

I tried to implement BST but std::nullptr shows me an error: error: expected unqualified-id before ‘nullptr’ #include #include template class BinTreeNode { public: BinTreeNode(T key): data {key} { …
user4833046
7
votes
2 answers

Troubles in implementing nullptr on VS2013

I try an official implementation of nullptr I know with nullptr and std::nullptr_t supported by Compilers, this implementation is meaningless.I am just trying to study C++. and everything works well by GCC4.9.1 on my PC, both -std=c++03 and…
ballypc
  • 71
  • 3
7
votes
3 answers

No side effect with comma operator, return statement, and nullptr?

I have the following test code: #include #include enum class Result : std::uint32_t {SUCCESS = 0, INSUCCESS = 1}; void* func(Result& result) { // works great /* result = Result::INSUCCESS; return NULL; */ …
haelix
  • 4,245
  • 4
  • 34
  • 56
7
votes
3 answers

Netbeans hating nullptr but still working fine

I've seen similar posts around about this but can't get Netbeans to just stop showing the error message "Unable to resolve identifier nullptr" all over my code when it's working fine. I have C++11 enabled properly, not sure what the problem is?
Instinct
  • 349
  • 1
  • 3
  • 10
7
votes
2 answers

In c++11, does dynamic_cast return nullptr or 0?

I want to check the result of dynamic_cast. In c++11 (or c++0x, for compilers that support nullptr), should I compare against nullptr or 0? Does it matter, and if so, why? Is the result compiler-dependent?
Patrick
  • 2,243
  • 2
  • 23
  • 32
6
votes
1 answer

Why does adding audio stream to ffmpeg's libavcodec output container cause a crash?

As it stands, my project correctly uses libavcodec to decode a video, where each frame is manipulated (it doesn't matter how) and output to a new video. I've cobbled this together from examples found online, and it works. The result is a perfect…
Sniggerfardimungus
  • 11,583
  • 10
  • 52
  • 97
6
votes
1 answer

What does comparing the result of the three-way comparison operator with nullptr do?

Given the example from cppreference on <=>, we can simplify the example code to: struct person { std::string name; std::string surname; auto operator <=> (const person& p) const { if (const auto result = name <=> p.name; result…
Fureeish
  • 12,533
  • 4
  • 32
  • 62
6
votes
1 answer

VS2019 C6011 Error Dereferencing Null Pointer 'NewNode'

Hello? I'm studying Doubly Linked List Anyway, when I used to put data on node, there were C6011 error [:Dereferencing Null Pointer 'NewNode'] So I refered https://learn.microsoft.com/ko-kr/visualstudio/code-quality/c6011?view=vs-2019 and tried to…
정지수
  • 95
  • 1
  • 6
6
votes
2 answers

Are objects of type nullptr_t ever needed?

Quoting C++11: (18.2/9) nullptr_t is defined as follows: namespace std { typedef decltype(nullptr) nullptr_t; } The type for which nullptr_t is a synonym has the characteristics described in 3.9.1 and 4.10. [ Note: Although nullptr’s address…
embedc
  • 1,485
  • 1
  • 6
  • 20
6
votes
2 answers

What C++17 standard say about calling delete on nullptr?

C++03 Standard say's: 5.3.5 Delete [...] In either alternative, if the value of the operand of delete is the null pointer the operation has no effect.[...] char *p = nullptr; delete p; //no effect It means, it is valid to delete null pointer in…
msc
  • 33,420
  • 29
  • 119
  • 214
6
votes
1 answer

Why does not std::nullptr_t work with std::cout in C++?

I learned about std::nullptr_t that is the type of the null pointer literal, nullptr. Then I made small program : #include int main() { std::nullptr_t n1; std::cout<
user7620837
6
votes
3 answers

Which Clang warning is equivalent to Wzero-as-null-pointer-constant from GCC?

Our project uses C++11/14, and we want to use nullptr instead of 0 or NULL with pointers, even when 0 (as an integer literal) is allowed. I have the following code: int main() { int *ptr1 = nullptr; // #1 int *ptr2 = 0; // #2 } If I…
Daniel
  • 2,657
  • 1
  • 17
  • 22