What is the range of illegle memory addresses?

Segher Boessenkool segher@kernel.crashing.org
Wed May 19 00:21:12 GMT 2021

On Tue, May 18, 2021 at 04:46:13PM -0500, Peter Bergner via Gcc-help wrote:
> On 5/18/21 4:35 PM, Peng Yu wrote:
> > So, to write a portable C program, no values other than NULL should be
> > used as an invalid memory address?
> I guess it depends on your definition of invalid.  NULL may be a special
> address used as a sentinel, but that doesn't mean every system will give
> you a SEGV if you happen to access NULL.  Some will happily let you do it.

You can also use the address of an actual object for this.  This is
standard for sentinel structs even: every pointer in a sentinel object
points to some (other or same) sentinel object, so that you can always
dereference without having to check for NULL first.  No non-sentinel
object will have the same address, so this works fine.

The only thing special about the address 0 is that it is cheaper to
test for than most other numbers, on most architectures.


More information about the Gcc-help mailing list