小于比较空指针 [英] less than comparison for void pointers

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

问题描述

我想比较两个这样的空指针:

I want to compare two void pointers like this:

void foo(void* p1, void* p2) {

  if (p1 < p2) {
    void *tmp = p1;
    p1 = p2;
    p2 = tmp;
  }

  // do something with p1 and p2.
}

根据标准,这正确吗?我的意思是比较void指针是一种定义明确的行为吗?

Is this correct according to the standard? I mean is the comparison of void pointers a well-defined behaviour?

如果有人能指出我所记录的C标准,我将不胜感激.

I'd appreciate if someone can point me to the C standard where this is documented.

推荐答案

正如Drew McGowen在评论中指出的那样,但是我将在此处引用报价:

As Drew McGowen pointed out in a comment, but I'll post the quote here:

6.5.8关系运算符

6.5.8 Relational operators

5比较两个指针时,结果取决于指针的地址空间中的相对位置. 指向的对象. 如果两个指向对象类型的指针都指向 相同的对象,或两者都指向同一对象的最后一个元素 数组对象,它们比较相等.如果指向的对象是 同一聚合对象的成员,指向稍后声明的结构成员的指针比指向声明的成员的指针大 在结构中处于较早的位置,并且指向具有较大数组元素的指针 下标值比较大于指向相同元素的指针 下标值较低的数组.所有指向成员的指针 同一联合对象比较相等.如果表达式P指向 数组对象的元素,表达式Q指向最后一个 相同数组对象的元素,指针表达式Q + 1比较 大于P.在所有其他情况下,行为都是不确定的.

5 When two pointers are compared, the result depends on the relative locations in the address space of the objects pointed to. If two pointers to object types both point to the same object, or both point one past the last element of the same array object, they compare equal. If the objects pointed to are members of the same aggregate object, pointers to structure members declared later compare greater than pointers to members declared earlier in the structure, and pointers to array elements with larger subscript values compare greater than pointers to elements of the same array with lower subscript values. All pointers to members of the same union object compare equal. If the expression P points to an element of an array object and the expression Q points to the last element of the same array object, the pointer expression Q+1 compares greater than P. In all other cases, the behavior is undefined.

这是来自C11标准的. C99相同.

That's from the C11 standard. C99 is the same.

C++11大致相同.关于指针转换的一些调整,如果需要,我可以将其全部粘贴.更重要的是,该行为是未指定的(如Chris所指出的那样).

From C++11, it's more or less the same. Some tweaks about pointer conversions, I can paste it all if you want. More importantly, the behaviour is Unspecified (as Chris pointed out above).

请注意,未定义行为是致命的.如果您比较两个无关的指针,您的机器可能会着火,发射核导弹,让恶魔从您的鼻子中飞出,等等.

Note that Undefined Behaviour is lethal. If you compare two unrelated pointers, your machine may catch fire, launch nuclear missiles, make demons fly out of your nose, etc.

未指定的行为必须做一些模糊合理的事情.编译器不必对其进行记录,甚至不必对两个不同的程序执行相同的操作,但是它不会使人震惊.您的程序仍然被认为是有效的.

Unspecified Behaviour has to do something vaguely reasonable. The compiler doesn't have to document it, or even do the same thing for two different programs, but it can't blow up the world. Your program is still considered valid.

因此在您的特殊情况下,编译为C时,用户可能会导致不确定的行为,具体取决于传递给函数的内容.这似乎很危险.

So in your particular case, compiling as C, the user could cause Undefined Behaviour depending on what they pass to the function. This seems pretty dangerous.

此外,与我对此问题的评论相反,您不能仅在任意两个void*上使用!=: C11

Also, contrary to my comment on the question, you cannot just use != on two arbitrary void*s: C11

6.5.9相等运算符

6.5.9 Equality operators

2符合下列条件之一:

2 One of the following shall hold:

-两个操作数都具有算术类型;

— both operands have arithmetic type;

-两个操作数都是指向兼容类型的合格或不合格版本的指针;

— both operands are pointers to qualified or unqualified versions of compatible types;

-一个操作数是指向对象类型的指针,另一个是指向对象类型的指针 无效的合格或不合格版本;或

— one operand is a pointer to an object type and the other is a pointer to a qualified or unqualified version of void; or

-一个操作数是一个指针,另一个是空指针常量.

— one operand is a pointer and the other is a null pointer constant.

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

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