指针变量是带有某些运算符的整数还是“符号"? [英] Are pointer variables just integers with some operators or are they "symbolic"?

查看:158
本文介绍了指针变量是带有某些运算符的整数还是“符号"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最初的单词选择令人困惑.术语符号"比原始的(神秘的")好得多.

The original word choice was confusing. The term "symbolic" is much better than the original ("mystical").

在讨论我以前的C ++问题时,有人告诉我指针是

In the discussion about my previous C++ question, I have been told that pointers are

  • "a simple value type much like an integer"
  • not "mystical"
  • "The Bit pattern (object representation) contains the value (value representation) (§3.9/4) for trivially copyable types, which a pointer is."

这听起来不是不是!如果没有什么是符号性的,并且指针是其表示形式,那么我可以执行以下操作.我可以吗?

This does not sound right! If nothing is symbolic and a pointer is its representation, then I can do the following. Can I?

#include <stdio.h>
#include <string.h>

int main() {
    int a[1] = { 0 }, *pa1 = &a[0] + 1, b = 1, *pb = &b;
    if (memcmp (&pa1, &pb, sizeof pa1) == 0) {
        printf ("pa1 == pb\n");
        *pa1 = 2;
    }
    else {
        printf ("pa1 != pb\n");
        pa1 = &a[0]; // ensure well defined behaviour in printf
    }
    printf ("b = %d *pa1 = %d\n", b, *pa1);
    return 0;
 }

这是一个C和C ++问题.

This is a C and C++ question.

使用GNU GCC v4.8.3在在线编译和执行C 中进行测试:gcc -O2 -Wall给出

Testing with Compile and Execute C Online with GNU GCC v4.8.3: gcc -O2 -Wall gives

pa1 == pb                                                                                                                                                                                       
b = 1 *pa1 = 2    

使用使用GNU GCC v4.8.3在线编译和执行C ++ :g++ -O2 -Wall

pa1 == pb                                                                                                                                                                                       
b = 1 *pa1 = 2        

因此通过(&a)[1]修改b的操作在C和C ++中使用GCC失败.

So the modification of b via (&a)[1] fails with GCC in C and C++.

当然,我想要一个基于标准报价的答案.

Of course, I would like an answer based on standard quotes.

为了回应对&a + 1上的UB的批评,现在a是1个元素的数组.

To respond to criticism about UB on &a + 1, now a is an array of 1 element.

相关:取消引用包含对象(数组数组)地址的超出范围的指针

附加说明:我认为,托尼·德尔罗伊(Tony Delroy)首次使用了神秘"一词

Additional note: the term "mystical" was first used, I think, by Tony Delroy here. I was wrong to borrow it.

推荐答案

C被认为是一种指针和整数紧密相关的语言,具体关系取决于目标平台.指针与整数之间的关系使该语言非常适合于低级或系统编程的目的.为了下面的讨论目的,我将这种语言称为低级C" [LLC].

C was conceived as a language in which pointers and integers were very intimately related, with the exact relationship depending upon the target platform. The relationship between pointers and integers made the language very suitable for purposes of low-level or systems programming. For purposes of discussion below, I'll thus call this language "Low-Level C" [LLC].

C标准委员会撰写了对不同语言的描述,虽然没有明确禁止这种关系,但是即使实现为目标生成代码,也没有以任何有用的方式认可这种关系.以及在这种情况下有用的应用领域.我将这种语言称为仅高级C" [HLOC].

The C Standards Committee wrote up a description of a different language, where such a relationship is not expressly forbidden, but is not acknowledged in any useful fashion, even when an implementation generates code for a target and application field where such a relationship would be useful. I'll call this language "High Level Only C" [HLOC].

在编写标准的日子里,大多数自称为C实现的事物都处理了LLC的方言.与HLOC相比,最有用的编译器处理的方言在更多情况下定义了有用的语义,但没有LLC那么多.指针的行为更像是整数,还是更像抽象的神秘实体,取决于使用的是哪种方言.如果正在进行系统编程,则将C视为将指针和整数视为紧密相关是合理的,因为适合该目的的LLC方言可以这样做,而不适合这样做的HLOC方言则不适合.但是,在进行高端数字运算时,使用HLOC的方言的频率会更高,而后者却无法识别这种关系.

In the days when the Standard was written, most things that called themselves C implementations processed a dialect of LLC. Most useful compilers process a dialect which defines useful semantics in more cases than HLOC, but not as many as LLC. Whether pointers behave more like integers or more like abstract mystical entities depends upon which exact dialect one is using. If one is doing systems programming, it is reasonable to view C as treating pointers and integers as intimately related, because LLC dialects suitable for that purpose do so, and HLOC dialects that don't do so aren't suitable for that purpose. When doing high-end number crunching, however, one would far more often being using dialects of HLOC which do not recognize such a relationship.

真正的问题和如此之多的根源在于,LLC和HLOC的分歧越来越大,而两者都被称为C.

The real problem, and source of so much contention, lies in the fact that LLC and HLOC are increasingly divergent, and yet are both referred to by the name C.

这篇关于指针变量是带有某些运算符的整数还是“符号"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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