空指针的地址? [英] Address of Null pointer?

查看:308
本文介绍了空指针的地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在整个宏观来到低于

#define OFFSETOF(TYPE, ELEMENT) ((size_t)&(((TYPE *)0)->ELEMENT))

我有种无法消化这一点,因为在C ++中,当我尝试尊重空指针,我期待一个意外的行为......但是怎么来它可以有一个地址?什么无效的地址呢?

I kind of not able to digest this because in c++, when I try to deference a null pointer, I expect an unexpected behaviour... but how come it can have an address? what does address of null mean?

推荐答案

有关宏的目的:
它假定有一个在地址0类型键入的对象,并返回其实际上是在结构成员的偏移量成员的地址。

For the purpose of the macro: It assumes that there is an object of type TYPE at address 0 and returns the address of the member which is effectively the offset of the member in the structure.

<一个href=\"http://stackoverflow.com/questions/2474018/when-does-invoking-a-member-function-on-a-null-instance-result-in-undefined-behav\">This答案解释了为什么这是不确定的行为。我认为这是最重要的话:

This answer explains why this is undefined behaviour. I think that this is the most important quote:

If E1 has the type "pointer to class X," then the expression E1->E2 is
converted to the equivalent form (*(E1)).E2; *(E1) will result in
undefined behavior with a strict interpretation, and .E2 converts it
to an rvalue, making it undefined behavior for the weak
interpretation.

这是这里的情况。虽然有人认为这是有效的。需要注意的是,这将产生虽然许多编译器正确的结果是很重要的。

which is the case here. Although others think that this is valid. It is important to note that this will produce the correct result on many compilers though.

这篇关于空指针的地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆