(C++) nullptr

January 14, 2018 ยท View on GitHub

nullptr is a C++11 keyword to indicate an unitialized pointer.

#include <cassert>

int main()
{
  //Create a new p
  int * p = new int(3);
  assert(*p == 3);

  //Get rid of the current p
  delete p;
  p = nullptr;

  //Create a new p
  p = new int(4);
  assert(*p == 4);
}

Advice

  • Prefer nullptr to NULL and 0 [2,3]

References