0Pricing
C++ Academy · Lesson

nullptr and Null Pointer Safety

Use nullptr instead of NULL or 0 and guard against null dereferences.

nullptr and Null Pointer Safety is a free C++ Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What is a Null Pointer?

A null pointer is a special value that points to "nothing." Dereferencing it is undefined behavior — usually crashes with a segmentation fault.

nullptr in Modern C++

Since C++11, the keyword nullptr represents a typed null pointer literal. Prefer it over NULL or 0.

int* p1 = nullptr;
int* p2 = NULL;       // legacy macro
int* p3 = 0;          // legacy literal

Why nullptr?

NULL is just a macro for 0. It can confuse overload resolution between int and int*. nullptr has the unique type std::nullptr_t.

void f(int);
void f(int*);

f(NULL);      // calls f(int) -- surprising!
f(nullptr);   // calls f(int*) -- correct

Checking for Null

Pointers convert implicitly to bool: non-null is true, null is false.

if (p) std::cout << "has value\n";
if (!p) std::cout << "is null\n";
if (p != nullptr) std::cout << "explicit form\n";

Initialize, Always

Uninitialized pointers contain garbage and pretend to point somewhere. Always initialize, even if just to nullptr.

int* p;            // BAD: undefined contents
int* q = nullptr;  // GOOD

Dereferencing Null

Reading or writing through a null pointer is undefined behavior. On most desktop systems it segfaults. On embedded devices it can corrupt memory silently.

Defensive Checks

Check before dereferencing in public APIs or when the caller might pass null.

void process(int* data) {
    if (!data) return;
    *data *= 2;
}

References Instead of Pointers

If null is never meaningful, use a reference parameter. The compiler guarantees the reference is bound to something.

// Before
void f(Widget* w);   // can be null?

// After
void f(Widget& w);   // must be a real widget

std::optional for Optional Values

C++17 added std::optional<T> for "maybe a value" semantics — clearer than a nullable pointer in non-allocation contexts.

#include <optional>
std::optional<int> find(int x);
if (auto v = find(42)) std::cout << *v;

Smart Pointers and Null

std::unique_ptr and std::shared_ptr can be null too. They convert to bool for checks and reset with = nullptr.

Pitfall: Dangling After Delete

After delete p, the pointer still holds the old address. Reset it: p = nullptr;. Smart pointers do this automatically.

Quick Check

Which null pointer literal should you use in modern C++?

Recap

Use nullptr for null pointer values. Always initialize pointers. Check before dereferencing or, better, use references and std::optional when null is never valid.

Frequently asked questions

Is the “nullptr and Null Pointer Safety” lesson free?

Yes — the full text of “nullptr and Null Pointer Safety” is free to read here on the web, and the C++ Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C++ Academy course, upgrade to CoddyKit PRO.

What will I learn in “nullptr and Null Pointer Safety”?

Use nullptr instead of NULL or 0 and guard against null dereferences. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C++ Academy?

No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “nullptr and Null Pointer Safety” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C++ Academy lesson?

Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Raw Pointers and the Address-of Operator
  2. Dereferencing and Pointer Arithmetic
  3. nullptr and Null Pointer Safety
  4. When to Use Pointers vs References
← Back to C++ Academy