typedef struct {
int a;
}stTemp_t;
int main()
{
stTemp_t *pstTemp = NULL;
int *p = &(pstTemp->a); // <<----- supposedly de-ref NULL pointer
return 0;
}
The instruction pointed above, i thought should've caused a segmentation fault which it does not. I tried omitting the default compiler optimization by using "gcc -O0".
Naturally enough, if I replace that with int i = pstTemp->a
, I get a seg-fault. I tried to run the above program throgh gdb to figure out what is going on & following is my observation -
(gdb) p pstTemp
$2 = (stTemp_t *) 0x0
(gdb) p pstTemp->a
Cannot access memory at address 0x0
(gdb) p &(pstTemp->a)
$3 = (int *) 0x0
here in $3
, we can see that when i try to print &(pstTemp->a)
, it seems to be interpreted as an address hence being equivalent to int *p = NULL
.
However my doubt is, shouldn't the statement (pstTemp->a)
get evaluated before the & takes effect & cause a seg-fault anyways?