問題描述
原始單詞選擇令人困惑.象征"一詞比原來的(神秘")好得多.
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
- "一個很像的簡單值類型一個整數"
- 不是神秘的"
- "位模式(對象表示) 包含普通可復制類型的值(值表示)(第 3.9/4 節),這是一個指針."
這聽起來不對!如果沒有什么是象征性的并且指針是它的表示,那么我可以執行以下操作.我可以嗎?
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
");
*pa1 = 2;
}
else {
printf ("pa1 != pb
");
pa1 = &a[0]; // ensure well defined behaviour in printf
}
printf ("b = %d *pa1 = %d
", b, *pa1);
return 0;
}
這是一個 C 和 C++ 問題.
This is a C and C++ question.
使用 在線編譯和執行 C 進行測試,使用 GNU GCC v4.8.3: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
使用 在線編譯和執行 C++ 進行測試,使用 GNU GCC v4.8.3:g++ -O2 -Wall
Testing with Compile and Execute C++ Online with GNU GCC v4.8.3: 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.
這篇關于指針變量只是帶有某些運算符的整數還是“符號"?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!