运营商如何<和>使用指针?

发布于 2024-10-10 16:12:46 字数 866 浏览 4 评论 0原文

只是为了好玩,我有一个 const char*std::list,每个元素都指向一个以 null 结尾的文本字符串,并运行了 std:: list::sort() 就可以了。碰巧的是,它(没有双关语)没有对字符串进行排序。考虑到它正在处理指针,这是有道理的。

根据 std::list::sort( 的 文档 ),它(默认情况下)使用元素之间的运算符 < 进行比较。

暂时忘记这个列表,我真正的问题是:这些 (>、<、>=、<=) 运算符如何处理 C++ 和 C 中的指针?他们只是比较实际的内存地址吗?

char* p1 = (char*) 0xDAB0BC47;
char* p2 = (char*) 0xBABEC475;

例如,在 32 位小端系统上,p1 > > p2 因为 0xDAB0BC47 > 0xBABEC475

测试似乎证实了这一点,但我认为最好将其放在 StackOverflow 上以供将来参考。 C 和 C++ 都做了一些 奇怪的事情到指针,所以你永远不知道......

Just for fun, I had a std::list of const char*, each element pointing to a null-terminated text string, and ran a std::list::sort() on it. As it happens, it sort of (no pun intended) did not sort the strings. Considering that it was working on pointers, that makes sense.

According to the documentation of std::list::sort(), it (by default) uses the operator < between the elements to compare.

Forgetting about the list for a moment, my actual question is: How do these (>, <, >=, <=) operators work on pointers in C++ and C? Do they simply compare the actual memory addresses?

char* p1 = (char*) 0xDAB0BC47;
char* p2 = (char*) 0xBABEC475;

e.g. on a 32-bit, little-endian system, p1 > p2 because 0xDAB0BC47 > 0xBABEC475?

Testing seems to confirm this, but I thought it'd be good to put it on StackOverflow for future reference. C and C++ both do some weird things to pointers, so you never really know...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

筱果果 2024-10-17 16:12:47

在 C++ 中,不能使用关系运算符来比较任何指针。您只能比较指向同一数组中的元素的两个指针或指向同一对象的成员的两个指针。 (当然,您也可以将指针与其自身进行比较。)

但是,您可以使用 std::less 和其他关系比较函数对象来比较任意两个指针。结果是实现定义的,但保证有总排序。

如果您有一个平面地址空间,则指针比较很可能只是将地址视为整数进行比较。

(我相信 C 中的规则是相同的,没有比较函数对象,但必须有人确认这一点;我对 C 的熟悉程度不如对 C++ 的熟悉程度。)

In C++, you can't compare just any pointers using the relational operators. You can only compare two pointers that point to elements in the same array or two pointers that point to members of the same object. (You can also compare a pointer with itself, of course.)

You can, however, use std::less and the other relational comparison function objects to compare any two pointers. The results are implementation-defined, but it is guaranteed that there is a total ordering.

If you have a flat address space, it's likely that pointer comparisons just compare addresses as if they are integers.

(I believe the rules are the same in C, without the comparison function objects, but someone will have to confirm that; I'm not nearly as familiar with C as I am with C++.)

森林迷了鹿 2024-10-17 16:12:47

这只是一个补充。

在 C++ 20.3.3/8 中:

对于模板更大、更少,
大于等于和小于等于
任何指针类型的特化
产生全序,即使
内置运算符 <、>、<=、>= do
不是。

在 C 6.5.8/5 中:

如果两个指向对象的指针或
不完整类型都指向
同一物体,或两者都指向过去
同一数组的最后一个元素
对象,它们比较相等。如果
指向的对象是成员
相同的聚合对象,指向
稍后声明的结构成员
比较大于指针
早些时候宣布的成员
结构体和数组指针
下标值较大的元素
比较大于指针
同一数组中具有较低值的元素
下标值。所有指针指向
同一联合对象的成员
比较相等。如果表达式 P
指向数组的一个元素
对象和表达式 Q 指向
同一数组的最后一个元素
对象,指针表达式 Q+1
比较大于 P。在所有其他中
情况下,行为未定义

因此,我认为比较问题中属于两个不同的 '\0' 终止字符串的 char const* 是一种未定义的行为(在 C 中)。

This is just a supplementation.

In C++ 20.3.3/8:

For templates greater, less,
greater_equal, and less_equal, the
specializations for any pointer type
yield a total order, even if the
built-in operators <, >, <=, >= do
not.

In C 6.5.8/5:

If two pointers to object or
incomplete 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
.

So, I think comparing char const* which belong to two different '\0'-terminated-string as in the question is an undefined behavior (in C).

瑕疵 2024-10-17 16:12:47

是的,他们只是比较内存地址。

Yes, they just compare memory address.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文