是指针变量只是一些运营商的整数或者是他们"神秘"? [英] Are pointer variables just integers with some operators or are they "mystical"?

查看:86
本文介绍了是指针变量只是一些运营商的整数或者是他们"神秘"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的previous C ++问题的讨论中,我已被告知,指针

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


  • <一个href=\"http://stackoverflow.com/questions/32043314/overwriting-an-object-with-an-object-of-same-type/32043636#comment51987347_32043314\">a简单的值类型很像一个整数

  • 不是<一个href=\"http://stackoverflow.com/questions/32043314/overwriting-an-object-with-an-object-of-same-type/32043636#comment51988103_32043636\">mystical\"

  • <一个href=\"http://stackoverflow.com/questions/32043314/overwriting-an-object-with-an-object-of-same-type/32043636#comment51987714_32043636\">The位模式(对象重新presentation)包含平凡拷贝的类型值(转口货值为presentation)(§3.9/ 4),其中一个指针。

  • "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."

这确实的的声音吧!如果没有什么神秘和指针是其重新presentation,那么我就可以做到以下几点。可以吗?

This does not sound right! If nothing is mystical 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

pa1 == pb                                                                                                                                                                                       
b = 1 *pa1 = 2    

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

pa1 == pb                                                                                                                                                                                       
b = 1 *pa1 = 2        

所以通过的变体B (安培;一)[1] 失败在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.

编辑:约UB回应批评的&安培A + 1 ,现在 A 是数组1元。

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

相关报道:<一href=\"http://stackoverflow.com/questions/32043795/dereferencing-an-out-of-bound-pointer-that-contains-the-address-of-an-object-ar\">Dereferencing一个出界的指针包含一个对象的地址(数组的数组)

推荐答案

要首先要说的是,在一个编译器生成code一个测试在一个建筑的样本是不是在此基础上得出一个结论在语言的行为。

The first thing to say is that a sample of one test on one compiler generating code on one architecture is not the basis on which to draw a conclusion on the behaviour of the language.

C ++(和c)是具有可移植的意图创建通用语言。即用C ++编写一个系统上的良好形成方案应该在任何其他运行(除了调用特定的系统服务)。

c++ (and c) are general purpose languages created with the intention of being portable. i.e. a well formed program written in c++ on one system should run on any other (barring calls to system-specific services).

曾几何时,由于各种原因,包括向后兼容性和成本,内存映射没有在所有处理器上是连续的。

Once upon a time, for various reasons including backward-compatibility and cost, memory maps were not contiguous on all processors.

例如我曾经写了一个6809系统,其中一半的内存是通过分页PIA在存储器映射的非分页部分讨论了关于code。我的C编译器能够应付这种因为指针是,对于编译器,它知道如何写一个PIA'神秘'的类型。

For example I used to write code on a 6809 system where half the memory was paged in via a PIA addressed in the non-paged part of the memory map. My c compiler was able to cope with this because pointers were, for that compiler, a 'mystical' type which knew how to write to the PIA.

80386家,其中有地址是16字节团体组织的寻址模式。查找 FAR 指针,你会看到不同的指针运算。

The 80386 family has an addressing mode where addresses are organised in groups of 16 bytes. Look up FAR pointers and you'll see different pointer arithmetic.

这是指针的发展在C ++中的历史。并非所有的芯片厂商已经表现良好的语言适应他们都(一般),而无需重写源$ ​​C ​​$ C。

This is the history of pointer development in c++. Not all chip manufacturers have been "well behaved" and the language accommodates them all (usually) without needing to rewrite source code.

这篇关于是指针变量只是一些运营商的整数或者是他们&QUOT;神秘&QUOT;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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