Pointers
Null Pointer
nullptr means a pointer is not pointing at an object.
nullptr
Check a pointer before dereferencing when it might be `nullptr`.
Null Pointer
null_pointer.cpp
Replay: real traced execution (multi-file project)
#include <iostream>
int main() {
bool hasValue = false;
int value = 42;
int fallback = -1;
int* maybeValue = nullptr;
if (hasValue) {
maybeValue = &value;
}
int result = fallback;
if (maybeValue != nullptr) {
result = *maybeValue;
}
std::cout << "hasValue=" << hasValue << std::endl;
std::cout << "result=" << result << std::endl;
return 0;
}
#include <iostream>
int main() {
bool hasValue = true;
int value = 42;
int fallback = -1;
int* maybeValue = nullptr;
if (hasValue) {
maybeValue = &value;
}
int result = fallback;
if (maybeValue != nullptr) {
result = *maybeValue;
}
std::cout << "hasValue=" << hasValue << std::endl;
std::cout << "result=" << result << std::endl;
return 0;
}
#include <iostream>
int main() {
bool hasValue = false;
int value = 7;
int fallback = -1;
int* maybeValue = nullptr;
if (hasValue) {
maybeValue = &value;
}
int result = fallback;
if (maybeValue != nullptr) {
result = *maybeValue;
}
std::cout << "hasValue=" << hasValue << std::endl;
std::cout << "result=" << result << std::endl;
return 0;
}
#include <iostream>
int main() {
bool hasValue = false;
int value = 99;
int fallback = -1;
int* maybeValue = nullptr;
if (hasValue) {
maybeValue = &value;
}
int result = fallback;
if (maybeValue != nullptr) {
result = *maybeValue;
}
std::cout << "hasValue=" << hasValue << std::endl;
std::cout << "result=" << result << std::endl;
return 0;
}
hasValue ← 0, value ← 42, fallback ← -1, maybeValue ← 0, result ← -1
3int main() {4 bool hasValue→ 0 = false; //@hasValue=true5 int value→ 42 = 42; //@value=7, 996 int fallback→ -1 = -1;7 int* maybeValue→ 0 = nullptr;89 if (hasValue) {10 maybeValue = &value;11 }1213 int result→ -1 = fallback-1;14 if (maybeValue != nullptr) {15 result = *maybeValue;16 }1718 std::cout << "hasValue=" << hasValue0 << std::endl;19 std::cout << "result=" << result-1 << std::endl;20 return 0;21}outputhasValue=0 result=-1
hasValue ← 1, value ← 42, fallback ← -1, maybeValue ← 0
3int main() {4 bool hasValue→ 1 = true;5 int value→ 42 = 42;6 int fallback→ -1 = -1;7 int* maybeValue→ 0 = nullptr;maybeValue ← ⟨addr A⟩
9if (hasValue1) {10 maybeValue→ ⟨addr A⟩ = &value42;11}result ← -1
13int result→ -1 = fallback-1;14if (maybeValue != nullptr) {result ← 42
13int result = fallback;14if (maybeValue⟨addr A⟩ != nullptr) {15 result→ 42 = *maybeValue⟨addr A⟩;16}std::cout << "hasValue=" << hasValue << std::endl;
18 std::cout << "hasValue=" << hasValue1 << std::endl;19 std::cout << "result=" << result42 << std::endl;20 return 0;21}outputhasValue=1 result=42
hasValue ← 0, value ← 7, fallback ← -1, maybeValue ← 0, result ← -1
3int main() {4 bool hasValue→ 0 = false;5 int value→ 7 = 7;6 int fallback→ -1 = -1;7 int* maybeValue→ 0 = nullptr;89 if (hasValue) {10 maybeValue = &value;11 }1213 int result→ -1 = fallback-1;14 if (maybeValue != nullptr) {15 result = *maybeValue;16 }1718 std::cout << "hasValue=" << hasValue0 << std::endl;19 std::cout << "result=" << result-1 << std::endl;20 return 0;21}outputhasValue=0 result=-1
hasValue ← 0, value ← 99, fallback ← -1, maybeValue ← 0, result ← -1
3int main() {4 bool hasValue→ 0 = false;5 int value→ 99 = 99;6 int fallback→ -1 = -1;7 int* maybeValue→ 0 = nullptr;89 if (hasValue) {10 maybeValue = &value;11 }1213 int result→ -1 = fallback-1;14 if (maybeValue != nullptr) {15 result = *maybeValue;16 }1718 std::cout << "hasValue=" << hasValue0 << std::endl;19 std::cout << "result=" << result-1 << std::endl;20 return 0;21}outputhasValue=0 result=-1