43. Safe Initialization

#include <iostream>
using namespace std;

int main() {
    cout << NULL << "\n";
	cout << nullptr;
}


Solution Details

  • nullptr is type-safe (std::nullptr_t).
     
  • NULL is a macro (typically defined as 0).
     
  • Both behave the same in boolean checks (if (p)), but nullptr avoids overload ambiguities.

     

👉 In simple words:

  • p1 = nullptr; → “This is a pointer with no address.”
  • p2 = NULL; → “This is just zero, used like a pointer.”

     

Significance for Embedded Developers

  • When initializing pointers to resources (UART, GPIO, DMA), prefer nullptr to avoid bugs.
     

Example:

 Uart* handle = nullptr; // safe default

if (handle) { /* use safely */ }

  • This makes firmware code clearer and safer than using NULL.

     
Loading...

Input

Expected Output

0 nullptr