C++ – Pointer is pointing to 0x1 – is checking for null valid

cgdbnull

In some of our code; we are getting a segmentation fault and the gdb stacktrace shows the pointer is pointing to 0x1. We have 3 instances of these segmentation faults and in each one; the pointer ends up pointing to 0x1.

I would like to recover 'gracefully' from this error; instead of SEGFAULT. I can't check for NULL; since that would be 0. Do I explicitly check for address 0x1?

This is on Linux using GCC3.4.2 (SLES9 machine)

Best Answer

Yes, the reason you're getting a pointer pointing to 0x1 is most likely because you're dereferencing a structure which is pointing to null:

struct some_struct* ptr = NULL;
char blah = ptr->foo;

And it happens that foo is at offset 1 from the start of the structure. So the math ends up being *(0+1).