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
0
votes
3 answers

Why is the ternary operator not the same as an if-else here?

I'm using TR1's std::function to implement a simple callback mechanism. If I don't want to get called back, I register nullptr as the callback handler. This compiles and works fine: void Foo::MessageHandlingEnabled( bool enable ){ if( enable ) …
dario_ramos
  • 7,118
  • 9
  • 61
  • 108
0
votes
2 answers

&* for raw pointer, iterator and ... std::nullptr_t

I have a template function that is enabled (through a std::enable_if) its parameter is a raw pointer, or has a std::iterator category or is a std::nullptr_t. In that function, a raw pointer (a data member) is set equal to the parameter, like that…
Vincent
  • 57,703
  • 61
  • 205
  • 388
0
votes
2 answers

c++ GDI Bitmap doesn't want to load

My name is Miguel, and I'm trying to get each single pixel in one .bmp, but so far, when i initialize the bitmap, it doesn't get any value, so i guess that i have initialized it wrong. This is my current code:(Snippet) Bitmap *PerlinImage; void…
Miguel P
  • 1,262
  • 6
  • 23
  • 48
-1
votes
1 answer

Why can I use std::nullptr_t without including any file in MSVC

Like this auto func() { std::nullptr_t p; p=nullptr; return p; } I can build this code in msvc, but error in gcc
-1
votes
3 answers

What is the best practice to check if an object is null in C++

I am working on a simple example. Let s say that I have an object Object my_object and I want to check if the object is null. Therefore, I instantiate the object: auto my_object = createMyObject(param_object_1); The idea, is to check whether the…
bellotas
  • 2,349
  • 8
  • 29
  • 60
-1
votes
3 answers

Ensuring compilation error while passing null pointer to a function

Let's say that I have a function which takes in pointers. int functionA(int* a, int* b) { ... } I can add null checks inside functionA. Is there a way that I can ensure that an error occurs on the compile time whenever nullptr is passed as a…
Abhash Kumar Singh
  • 1,157
  • 2
  • 12
  • 22
-1
votes
1 answer

C++: Read access violation (this-> x was nullptr) with Blackmagic DeckLink SDK

I write a little program depending on the Blackmagic Decklink SDK. When I use the DeckLinkInput interface I get the read access violation message "this->dl_input was nulltr". After hours of debugging I have no idea how to fix this issue. Maybe one…
Polo
  • 23
  • 7
-1
votes
2 answers

Workaround for null pointer dereference?

I am answering a programming question on hackerrank, therefore the class code and anything outside the post-order function is not permitted. The question was a simple post-order traversal question. I am using c++ after some time, and since the class…
-1
votes
1 answer

Reaching nullptr crashes program - Binary Search Tree

I wonder if there is any way (I think there is) to avoid program crash when while loop is reaching nullptr? I did method that passes to string values from Binary Search Tree, but the problem occures when there is no right or left child of parent. My…
GSaple
  • 15
  • 6
-1
votes
1 answer

Why do we have nullptr but not nullchar?

My understanding - and I'm sure I'm about to learn - is that nullptr was added to C++ to formalise the convention that a zero value for a pointer means the pointer does not point to a valid object. Is there a case for (or against) adding a…
bythescruff
  • 1,869
  • 2
  • 17
  • 33
-1
votes
1 answer

"this was nullptr" error in C++. Linked List problems

I'm attempting to learn about Linked Lists, as well as getting the hang of pointers. Right now, the program I am working on is sorting names in a linked list. Basically, the user would enter a name, and that name gets sent between two other items…
Apollo503
  • 13
  • 6
-1
votes
1 answer

"No viable overloaded =" nullptr

I have just started with C++ and am stuck on the move constructor. Here is my .cpp: SimpleMatrix::SimpleMatrix(SimpleMatrix &&other_mat) { cols = other_mat.cols; rows = other_mat.rows; data_ = other_mat.data_; other_mat.cols = 0; other_mat.rows…
-1
votes
1 answer

Trying to loop check for Nullptr C++

I am trying to figure out how to alternatively check if the pointer in the array is currently NULL and loop through. At the moment if the content in that part of the array is deleted it will throw an error when looping. This array is initialized:…
T Young
  • 3
  • 2
-1
votes
4 answers

nullptr not declared when using -std=c++0x

Many other questions address similar issues, but I have yet to find an answer to this specific issue. I am getting the following error: error: ‘nullptr’ was not declared in this scope When compiling with the following command: g++ -std=gnu++0x…
treyhakanson
  • 4,611
  • 2
  • 16
  • 33
-1
votes
2 answers

'new' doesn't allocate memory to pointer which is a data member of a class

I've declared a pointer to hold a dynamic 2D array, and allocated memory to it using 'new' in class constructor but it is always equal to nullptr when checked using if statement. The code goes like this: class A { private: int* a; int d1,…