我遇到了下面的一段代码,它找到了结构中成员的偏移量。但是我无法弄清楚为什么它不会崩溃,即使它试图取消引用 NULL 指针
struct a_
{
int a;
int b;
};
int main()
{
int offset = &(((struct a_ *) 0x0)->b);
printf ("offset of b = %x\n", offset);
}
Output = 4
最佳答案
However I am not able to make out why it does not crash, even though it tries to derefer a NULL pointer.
您正在形成一个无效地址,这是未定义的行为,但由于 &
运算符取消了取消引用,因此没有取消引用 NULL 指针。
关于c - 为什么这段代码能够访问空指针而不会导致崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29876707/