在C指针比较是他们带符号? [英] Pointer comparisons in C. Are they signed or unsigned?

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

问题描述

您好我敢肯定,这必须是一个常见的​​问题,但我无法找到答案,当我搜索。我的问题基本上是关于两个指针。我想比较它们的地址,并确定,如果一个比另一个大。我希望所有的地址比较期间无符号。这是真的吗,它C89,C99和C ++之间有什么不同?当我用gcc编译比较是无符号。

Hi I'm sure this must be a common question but I can't find the answer when I search for it. My question basically concerns two pointers. I want to compare their addresses and determine if one is bigger than the other. I would expect all addresses to be unsigned during comparison. Is this true, and does it vary between C89, C99 and C++? When I compile with gcc the comparison is unsigned.

如果我有两个指针,我比较喜欢这样的:

If I have two pointers that I'm comparing like this:

char *a = (char *) 0x80000000; //-2147483648 or 2147483648 ?  
char *b = (char *) 0x1; 

然后 A 更大。这是一个标准的保障?结果
结果
结果

Then a is greater. Is this guaranteed by a standard?


修改以什么我试图做更新。我有一个情况,我想,以确定是否有算术错误不会导致指针就会出界。现在我有数组和结束地址的起始地址。如果有一个错误,指针计算是错误的,而且内存阵列的有效地址以外,我想,以确保没有发生访问冲突。我相信,如果它是该阵列的可接受的范围内,我可以通过比较疑指针,它已被另一个函数返回,并且确定prevent此。阴性和阳性地址的问题,与我是否可以做比较,如上面我原来的问题讨论的事情。

Edit to update on what I am trying to do. I have a situation where I would like to determine that if there's an arithmetic error it will not cause a pointer to go out of bounds. Right now I have the start address of the array and the end address. And if there's an error and the pointer calculation is wrong, and outside of the valid addresses of memory for the array, I would like to make sure no access violation occurs. I believe I can prevent this by comparing the suspect pointer, which has been returned by another function, and determining if it is within the acceptable range of the array. The question of negative and positive addresses has to do with whether I can make the comparisons, as discussed above in my original question.

我AP preciate的答案为止。根据我的编辑,你会说,我在做什么是GCC和MSVC未定义的行为?这是将在Microsoft Windows只。

I appreciate the answers so far. Based on my edit would you say that what I'm doing is undefined behavior in gcc and msvc? This is a program that will run on Microsoft Windows only.

下面是一个简单的过例如:

Here's an over simplified example:

char letters[26];  
char *do_not_read = &letters[26];  
char *suspect = somefunction_i_dont_control(letters,26);  
if( (suspect >= letters) && (suspect < do_not_read) )  
    printf("%c", suspect);  

结果
结果
另一个编辑阅读AndreyT的回答后,这似乎是正确的。因此,我会做这样的事情:



Another edit, after reading AndreyT's answer it appears to be correct. Therefore I will do something like this:

char letters[26];  
uintptr_t begin = letters;  
uintptr_t toofar = begin + sizeof(letters);  
char *suspect = somefunction_i_dont_control(letters,26);  
if( ((uintptr_t)suspect >= begin) && ((uintptr_t)suspect < toofar ) )
    printf("%c", suspect);  

结果
谢谢大家!


Thanks everyone!

推荐答案

指针比较,不能带符号。指针不是整数。

Pointer comparisons cannot be signed or unsigned. Pointers are not integers.

C语言(以及C ++)仅定义为指向到同一集合体(结构或阵列)的指针的指针比较。排序是天然:即在一个数组指向一个元件具有较小索引的指针较小。指向早先声明结构成员的指针较小。这就是它。

C language (as well as C++) defines pointer comparisons only for pointers that point into the same aggregate (struct or array). The ordering is natural: the pointer that points to an element with smaller index in an array is smaller. The pointer that points to a struct member declared earlier is smaller. That's it.

您不能合法使用C / C比较任意球++。这种比较的结果没有定义。如果你有兴趣在比较存储在指针的地址的数值,它是你的责任,以手动转换指针先整数值。在这种情况下,你将不得不决定是否使用符号或无符号整型(使用intptr_t uintptr_t形式)。根据您选择的类型,比较会签署或签名。

You can't legally compare arbitrary pointers in C/C++. The result of such comparison is not defined. If you are interested in comparing the numerical values of the addresses stored in the pointers, it is your responsibility to manually convert the pointers to integer values first. In that case, you will have to decide whether to use a signed or unsigned integer type (intptr_t or uintptr_t). Depending on which type you choose, the comparison will be "signed" or "unsigned".

这篇关于在C指针比较是他们带符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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