Null Pointer Dereferencing

Debates on dereferencing null pointers in C/C++ causing undefined behavior, compiler optimizations assuming no null derefs, and the representation of NULL pointers across platforms.

➡️ Stable 0.5x Programming Languages
2,365
Comments
19
Years Active
5
Top Authors
#2618
Topic ID

Activity Over Time

2008
11
2009
26
2010
38
2011
50
2012
33
2013
100
2014
107
2015
187
2016
143
2017
156
2018
201
2019
132
2020
159
2021
146
2022
199
2023
235
2024
207
2025
208
2026
27

Keywords

e.g TL std.org C99 NULL SEGFAULT EDIT UB DR ABI null pointer undefined undefined behavior pointers invalid ub address compiler behavior

Sample Comments

Karliss Jan 16, 2020 View on HN

You are not allowed to dereference a null pointer no matter how you use it even if you later convert it back to address.

mastax Jan 24, 2022 View on HN

Dereferencing a null pointer is Undefined Behavior.

Skunkleton Aug 29, 2019 View on HN

Comparing a pointer to null isn't UB. De-referencing invalid pointers is ub.

ArkyBeagle May 30, 2016 View on HN

If you never dereference a null pointer, then there will be no UB of that type.

pcwalton Oct 7, 2014 View on HN

Dereferencing a null pointer (i.e. use) is undefined behavior.

AlotOfReading Sep 5, 2024 View on HN

No compiler I'm aware of implements non-zero null pointers on systems where address 0 is valid (e.g. armv7), so it ends up being kind of a moot point.

raarts Feb 14, 2017 View on HN

* No NULL pointer can be dereferenced.NULL pointers CAN be dereferenced, it all depends on the environment you run on.

gpderetta Feb 14, 2017 View on HN

In standard C NULL pointers cannot be dereferenced. Full stop, there is nothing to argue about it.There are environments that either lack memory protection and do not allow invalid pointer derereferences to be caught, which means that you can't rely on the MMU to catch mistakes. In this case either deal with silent errors or get a compiler that is able (at a cost) to sanitize any pointer access.There are other systems in which memory address 0 is a perfectly valid address. The ABI of

Jiro Jan 3, 2023 View on HN

Are you sure the compiler didn't say "since having a null pointer gives undefined behavior, we can optimize out the part that confirms the address is non-null"?

EpicEng Jan 6, 2017 View on HN

If the pointer is null and you go ahead and dereference it anyway then yes, that's dumb, and the compiler is free to remove the null check.