c - C 中指针比较如何工作?可以比较不指向同一数组的指针吗?

标签 c pointers heap-memory undefined-behavior memory-layout

在 K&R(C 编程语言第二版)第 5 章中,我读到了以下内容:

First, pointers may be compared under certain circumstances. If p and q point to members of the same array, then relations like ==, !=, <, >=, etc. work properly.

这似乎意味着只能比较指向同一数组的指针。

但是当我尝试这段代码时

    char t = 't';
    char *pt = &t;
    char x = 'x';
    char *px = &x;

    printf("%d\n", pt > px);

1打印到屏幕上。

首先,我认为我会得到未定义或某种类型或错误,因为 ptpx不指向同一个数组(至少在我看来)。

也是 pt > px因为两个指针都指向栈上存储的变量,而栈是向下增长的,所以t的内存地址大于 x ?这就是为什么pt > px是真的吗?

当引入 malloc 时,我变得更加困惑。在第 8.7 章的 K&R 中也写了以下内容:

There is still one assumption, however, that pointers to different blocks returned by sbrk can be meaningfully compared. This is not guaranteed by the standard which permits pointer comparisons only within an array. Thus this version of malloc is portable only among machines for which the general pointer comparison is meaningful.

将指向堆上分配的空间的指针与指向堆栈变量的指针进行比较没有问题。

例如,以下代码运行良好,使用 1正在打印:

    char t = 't';
    char *pt = &t;
    char *px = malloc(10);
    strcpy(px, pt);
    printf("%d\n", pt > px);

根据我对编译器的实验,我认为任何指针都可以与任何其他指针进行比较,无论它们单独指向哪里。此外,我认为两个指针之间的指针算术很好,无论它们分别指向哪里,因为算术只是使用指针存储的内存地址。

尽管如此,我对在 K&R 上读到的内容感到困惑。

我问这个问题是因为我的教授。实际上把它变成了一个考试题。他给出了以下代码:

struct A {
    char *p0;
    char *p1;
};

int main(int argc, char **argv) {
    char a = 0;
    char *b = "W";
    char c[] = [ 'L', 'O', 'L', 0 ];

   struct A p[3];
    p[0].p0 = &a;
    p[1].p0 = b;
    p[2].p0 = c;

   for(int i = 0; i < 3; i++) {
        p[i].p1 = malloc(10);
        strcpy(p[i].p1, p[i].p0);
    }
}

What do these evaluate to:

  1. p[0].p0 < p[0].p1
  2. p[1].p0 < p[1].p1
  3. p[2].p0 < p[2].p1

答案是0 , 1 ,和0 .

(我的教授确实在考试中包含了免责声明,即问题适用于 Ubuntu Linux 16.04、64 位版本编程环境)

(编者注:如果允许更多标签,那么最后一部分将保证 ,也许 。如果问题/类的重点是特定的低级操作系统实现细节,而不是比可移植 C.)

最佳答案

根据C11 standard ,关系运算符< , <= , > ,和>=只能用于指向同一数组或结构对象的元素的指针。第 6.5.8p5 节对此进行了详细说明:

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.

请注意,任何不满足此要求的比较都会调用 undefined behavior ,这意味着(除其他外)您不能依赖结果是可重复的。

在您的特定情况下,对于两个局部变量的地址之间以及本地地址和动态地址之间的比较,该操作似乎“有效”,但是结果可能会通过进行看似不相关的更改而改变到您的代码,甚至使用不同的优化设置编译相同的代码。对于未定义的行为,仅仅因为代码可能崩溃或生成错误并不意味着它

例如,在 8086 实模式下运行的 x86 处理器具有分段内存模型,使用 16 位段和 16 位偏移量来构建 20 位地址。因此在这种情况下,地址不会完全转换为整数。

相等运算符==!=但没有这个限制。它们可以在任意两个指向兼容类型的指针或 NULL 指针之间使用。所以使用==!=在您的两个示例中都会生成有效的 C 代码。

但是,即使有==!=你可能会得到一些意想不到但仍然明确的结果。请参阅Can an equality comparison of unrelated pointers evaluate to true?了解更多详细信息。

关于你的教授给出的考试问题,它做出了一些有缺陷的假设:

  • 存在平面内存模型,其中地址和整数值之间存在一一对应关系。
  • 转换后的指针值适合整数类型。
  • 该实现在执行比较时只是将指针视为整数,而不利用未定义行为所提供的自由。
  • 使用堆栈并且局部变量存储在其中。
  • 堆用于从中提取分配的内存。
  • 堆栈(以及局部变量)出现在比堆(以及分配的对象)更高的地址。
  • 该字符串常量出现在比堆更低的地址处。

如果您要在不满足这些假设的架构和/或编译器上运行此代码,那么您可能会得到截然不同的结果。

此外,这两个示例在调用 strcpy 时也表现出未定义的行为。 ,因为右操作数(在某些情况下)指向单个字符而不是空终止字符串,导致函数读取超出给定变量的范围。

关于c - C 中指针比较如何工作?可以比较不指向同一数组的指针吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59516485/

相关文章:

CUnit断言断言 `((void *)0) != f_pCurSuite'失败

c++ - 使用相同类型的静态非空指针初始化后,指向自定义类型的静态指针保持 nullptr

c - C语言用指针交换一个数组与另一个数组的索引

c# - 字符串如何在堆中分配内存?

java - 让 R 从 Java 将数据加载到变量中,而无需 R 将信息发送回 Java

c - 在运行时转储 gcov 数据

c++ - pthread 在分离线程上创建错误 11

c - 在 C 中使用 libcurl 登录谷歌

c - 在方法头中后缀变量类型的星号有何意义?

C++ 处理堆上的对象