==为指针比较 [英] == for pointer comparison

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

问题描述

我是从C程序设计语言由Kernighan的&放大器报价;里奇:

I quote from "The C Programming Language" by Kernighan & Ritchie:

任何指针可以进行​​有意义的比较与零相等或不等。但是,行为是不确定的算术或比较与不指向同一个数组的成员指针。 (有一个例外:过去一​​个数组末尾的第一个元素的地址可在指针运算中使用)

Any pointer can be meaningfully compared for equality or inequality with zero. But the behavior is undefined for arithmetic or comparisons with pointers that do not point to members of the same array. (There is one exception: the address of the first element past the end of an array can be used in pointer arithmetic.)

这是否意味着我不能依靠 == 要求检查不同指针的平等?什么情况下,在这种比较会导致错误的结果?

Does this mean I cannot rely on == for checking equality of different pointers? What are the situations in which this comparison leads to a wrong result?

推荐答案

一个例子,在我脑海中是哈佛架构,为code和数据独立的地址空间。在这种架构编译器可以存储电脑的在code存储器常数的数据。由于这两个地址空间是分开的,一个指向在code存储器中的地址的可能的在数值上等于在数据存储器中的指针,不指向同一地址

One example that comes to my mind is Harvard architecture with separate address spaces for code and for data. In computers of that architecture the compiler can store constant data in the code memory. Since the two address spaces are separate, a pointer to an address in the code memory could be numerically equal to a pointer in the data memory, without pointing to the same address.

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

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