比较两个空指针到C ++中定义的不同对象? [英] Is comparing two void pointers to different objects defined in C++?

查看:177
本文介绍了比较两个空指针到C ++中定义的不同对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

受到有关动态投射到 void * 此回答的启发, :

Inspired by this answer about dynamic cast to void*:


...
bool eqdc(B* b1, B *b2) {
    return dynamic_cast<void*>(b1) == dynamic_cast<void*>(b2);
}
...
int main() {
    DD *dd = new DD();
    D1 *d1 = dynamic_cast<D1*>(dd);
    D2 *d2 = dynamic_cast<D2*>(dd);
    ... eqdc(d1, d2) ...


我想知道是否在C ++中完全定义的行为(根据03或11标准)比较两个指向有效的(in)相等的void指针

I am wondering if it is fully defined behaviour in C++ (according to the 03 or 11 standard) to compare two void pointers for (in)equality that point to valid, but different objects.

更一般而言,但可能不相关,是比较( == code>或!= )类型 void * 的两个值总是定义,保存指向有效对象/内存区域的指针?

More generally, but possibly not as relevant, is comparing (==or !=) two values of type void* always defined, or is it required that they hold a pointer to a valid object/memory area?

推荐答案

C说:


两个指针当且仅当两者都是空指针时才比较相等,都是指向
同一对象的指针(包括指向一个对象和一个子对象的指针)函数,
都是指向同一数组对象的最后一个元素的指针,或者一个是指针
指向一个数组对象的末尾,另一个是指向一个数组对象的开始的指针不同的
数组对象,它紧跟在地址
空间中的第一个数组对象后面。

Two pointers compare equal if and only if both are null pointers, both are pointers to the same object (including a pointer to an object and a subobject at its beginning) or function, both are pointers to one past the last element of the same array object, or one is a pointer to one past the end of one array object and the other is a pointer to the start of a different array object that happens to immediately follow the first array object in the address space.


两个相同类型的指针比较等于
,只有当它们都为null时,都指向同一个函数,或两者都代表相同的地址。

Two pointers of the same type compare equal if and only if they are both null, both point to the same function, or both represent the same address.

因此,这将意味着:

a)


它是C ++中完全定义的行为(根据03或11标准)比较两个void指针)等于指向有效但不同的对象。

it is fully defined behaviour in C++ (according to the 03 or 11 standard) to compare two void pointers for (in)equality that point to valid, but different objects.

所以,在C和C ++。你可以比较它们,在这种情况下,如果他们指向同一个对象,他们应该比较为真。这很简单。

So yes, in both C and C++. You can compare them and in this case they shall compare as true iff they point to the same object. That's simple.

b)


)两个类型为void *的值总是定义的,还是需要它们包含指向有效对象/内存区域的指针?

is comparing (==or !=) two values of type void* always defined, or is it required that they hold a pointer to a valid object/memory area?

同样,比较是明确定义的(标准说如果且仅当),因此每个比较的两个指针是明确定义的)。但是...

Again, the comparison is well-defined (standard says "if and only if" so every comparison of two pointers is well-defined). But then...


  • C ++以地址方式说话,所以我认为这意味着标准要求这个工作我们希望,

  • C,但是,要求两个指针都为null,或指向一个对象或函数,或一个元素通过一个数组对象。这,如果我的阅读技能没有关闭,意味着如果在给定平台上有两个指针具有相同的值,但没有指向一个有效的对象(例如未对齐),比较它们应该被良好定义并产生假。

这是令人惊讶的!

这是不是GCC的工作原理

int main() {
    void* a = (void*)1; // misaligned, can't point to a valid object
    void* b = a;
    printf((a == b) ? "equal" : "not equal");
    return 0;
}

结果:

equal

也许是C的UB有一个指针不是一个空指针,并且不指向一个对象,子对象或一个超过数组中的最后一个对象? Hm ...这是我的猜测,但是我们有了:

Maybe it's UB in C to have a pointer which isn't a null pointer and doesn't point to an object, subobject or one past the last object in an array? Hm... This was my guess, but then we have that:


一个整数可以转换为anypointer类型。除非以前指定,
结果是实现定义的,可能不正确对齐,可能不指向引用类型的
实体,并且可能是陷阱表示。

An integer may be converted to anypointer type. Except as previously specified, the result is implementation-defined, might not be correctly aligned, might not point to an entity of the referenced type, and might be a trap representation.

所以我只能解释它上面的程序是定义明确的,C标准期望它打印不相等,而GCC不真正遵守标准,但给出更直观的结果。

So I can only interpret it that the above program is well-defined and the C standard expects it to print "not equal", while GCC doesn't really obey the standard but gives a more intuitive result.

这篇关于比较两个空指针到C ++中定义的不同对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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