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

Returning an empty string literal VS. returning a nullptr - Are they the same?

I recently came across the following function in the public SDK of an application that I work on: virtual char* ExtentName() {return "";} When I compile the application using Visual Studio with the /permissive- flag, the function above causes the…
user3266738
  • 477
  • 2
  • 12
0
votes
2 answers

Endless loop with Bank Simulation

Hi when I run the bank simulation it gets caught in an endless loop on the last customer unable to remove the event from the list I don't know how to rework the logic to fix this issue. MY current function protects removing the first event before…
user9767719
0
votes
0 answers

Why does feeding a nullptr to std::ostream produce a signal 11, aka segmentation fault, from the OS, and how do I handle it?

I have a Date class with a Date::print method, that I'm attempting to test. The method itself is defined as void Date::print(std::ostream *printStream) const { invariant(); *printStream << day_ << '.' << month_ << '.' << year_; } and the test…
sesodesa
  • 1,473
  • 2
  • 15
  • 24
0
votes
2 answers

Array of Pointers to an Abstract Class: to nullptr or not to nullptr (C++)

I want to loop through an array of pointers to an abstract class to find an "empty" slot, that is to check whether an element points to an object of a derived class or not. My approach is to create the array and set each element to nullptr. Then, I…
Kaleb Coberly
  • 420
  • 1
  • 4
  • 19
0
votes
4 answers

c++: Is `this == nullptr` safe in member functions?

To calculate the height of a binary tree, height(left)+height(right)+1 is used and the height of non-existent nodes are defined as -1. So, if left or right is null, then the formula works without specifying these cases explicitly. I implemented this…
jnbrq -Canberk Sönmez
  • 1,790
  • 1
  • 17
  • 29
0
votes
0 answers

Is it undefined behavior to dereference nullptr like this?

#include struct A { int arr[666]; }; int main() { std::cout<(nullptr))->arr); //OK? } The code above could be run without error. Does standard say anything about such situation?
choxsword
  • 3,187
  • 18
  • 44
0
votes
0 answers

Pointer being invalidated?

I'm having the problem of my pointers/objects being invalidated. I have a class which is holding an std::vector< MyStruct* >. They're being stored in a message handler function which accessess the std::vector directly. Here's the function:…
0
votes
2 answers

Can nullptr be used as a type?

I was learning about the usage of enable_if and I stumbled upon the following code. template ::value, T>::type* = nullptr> void do_stuff(T& t) { …
Jaebum
  • 1,397
  • 1
  • 13
  • 33
0
votes
2 answers

nullptr not breaking loop over zero terminated string

I tried using following code sample given in Tour of C++ which uses nullptr to break loop over zero terminated string. However, my sample program doesn't seem to stop in the loop. Excerpt from the book: first version of code from book: ``` int…
0
votes
1 answer

Exception thrown: write access violation. this was nullptr

So I am trying to make a buffer class. This buffer class contains a huge buffer of size 384*4. The plan was for every UDP datagram received, size(384), the buffer class is called and return a pointer to where the datagram should be written. And…
Ulala
  • 31
  • 1
  • 6
0
votes
0 answers

2d dynamic allocation memory arror

#include #include #include int main(void) { int sizestr, sizecol, **arr; printf("Sizestr is "); scanf_s("%d", &sizestr); printf("Sizecol is "); scanf_s("%d", &sizecol); arr =…
Supy
  • 1
0
votes
1 answer

Why Does this particular code throw an exeption?

Critical error detected c0000374 #pragma once typedef struct node { int value; node* next; node* before; } node; void print_nodes(node* list) { node *current = (node*)malloc(sizeof(node)); //current->value = 0; current->next = list; while…
0
votes
0 answers

visual studio said right_data is nullptr but I can't find it

I'm doing my homework and the homework is making calculator with prefix and stack which is implemented by linkedlist. So I made stack myself, but code does not work. Visual studio said: right_data is nullptr. But no sign of line so I can't find…
Shwan
  • 17
  • 2
0
votes
1 answer

Why can't I set values of data for a pointer pointing to null

struct avail { int value; uint64_t y[8][5]; avail **masks; }; avail *n = new avail; n->masks = new avail*[48]; Now say I have to set some data of n->masks[i]->y[1][3]=0x000000000000000F Why can't I do this if I do n->masks[i]=NULL; I…
0
votes
0 answers

Check for nullptr Causes CTD

I have a block of code that I put in place to check for errors after a function runs. But this block of code is itself causing problems. ErrorType *error; CreateObject(..., &error); // Function argument is ErrorType** if (error) // nullptr if no…
TChapman500
  • 139
  • 13