問題描述
Stroustrup 的 C++ 編程語言第 3 版說,
The C++ Programming Language 3rd edition by Stroustrup says that,
指針的減法只有在兩個指針都指向時才定義同一個數組的元素(雖然語言沒有快速的方法確保情況如此).當從另一個指針中減去一個指針時,結果是兩個指針之間的數組元素數(一個整數).可以將一個整數加到一個指針上或減去一個來自指針的整數;在這兩種情況下,結果都是一個指針值.如果該值不指向與原始指針或超出的指針,使用該值的結果是未定義.
Subtraction of pointers is defined only when both pointers point to elements of the same array (although the language has no fast way of ensuring that is the case). When subtracting one pointer from another, the result is the number of array elements between the two pointers (an integer). One can add an integer to a pointer or subtract an integer from a pointer; in both cases, the result is a pointer value. If that value does not point to an element of the same array as the original pointer or one beyond, the result of using that value is undefined.
例如:
void f ()
{
int v1 [10];
int v2 [10];
int i1 = &v1[5] - &v1[3]; // i1 = 2
int i2 = &v1[5] - &v2[3]; // result undefined
}
我在維基百科上閱讀了關于未指明行為的內容.它說
I was reading about unspecified behavior on Wikipedia. It says that
在 C 和 C++ 中,只有當指針指向同一對象的成員或同一數組的元素時,才嚴格定義指向對象的指針的比較.
In C and C++, the comparison of pointers to objects is only strictly defined if the pointers point to members of the same object, or elements of the same array.
示例:
int main(void)
{
int a = 0;
int b = 0;
return &a < &b; /* unspecified behavior in C++, undefined in C */
}
所以,我很困惑.哪一個是正確的?維基百科或 Stroustrup 的書?C++ 標準對此有何規定?
So, I am confused. Which one is correct? Wikipedia or Stroustrup's book? What C++ standard says about this?
如果我誤解了什么,請糾正我.
Correct me If I am misunderstanding something.
推薦答案
注意,指針減法和指針比較是不同的操作,不同的規則.
Note that pointer subtraction and pointer comparison are different operations with different rules.
C++14 5.6/6,關于減指針:
C++14 5.6/6, on subtracting pointers:
除非兩個指針都指向同一個數組對象的元素或指向數組對象最后一個元素之后的元素,否則行為未定義.
Unless both pointers point to elements of the same array object or one past the last element of the array object, the behavior is undefined.
C++14 5.9/3-4:
C++14 5.9/3-4:
比較指向對象的指針定義如下:
Comparing pointers to objects is defined as follows:
如果兩個指針指向同一數組的不同元素或其子對象,則指向下標較高的元素的指針比較大.
If two pointers point to different elements of the same array, or to subobjects thereof, the pointer to the element with the higher subscript compares greater.
如果一個指針指向數組的一個元素或其子對象,而另一個指針指向數組的最后一個元素,則后一個指針比較大.
If one pointer points to an element of an array, or to a subobject thereof, and another pointer points one past the last element of the array, the latter pointer compares greater.
如果兩個指針遞歸地指向同一對象的不同非靜態數據成員,或指向此類成員的子對象,則指向后聲明成員的指針比較大,前提是這兩個成員具有相同的訪問控制和前提是他們的班級不是聯合.
If two pointers point to different non-static data members of the same object, or to subobjects of such members, recursively, the pointer to the later declared member compares greater provided the two members have the same access control and provided their class is not a union.
如果兩個操作數 都產生 If two operands 這篇關于C++ 中的指針比較是未定義或未指定的行為嗎?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!p
和 q
比較相等(5.10),p<=q
和 p>=q
都產生 true
和 p
和
p>q
都產生假.否則,如果指針 p
比較大于指針 q
,p>=q
,p>q
,q<=p
和 q
true
和 p<=q
, p
、
q>=p
和 q>p
都會產生 false
.否則,每個運算符的結果都是未指定的.p
and q
compare equal (5.10), p<=q
and p>=q
both yield true
and p<q
and p>q
both yield false. Otherwise, if a pointer p
compares greater than a pointer q
, p>=q
, p>q
, q<=p
, and q<p
all yield true
, and p<=q
, p<q
, q>=p
, and q>p
all yield false
. Otherwise, the result of each of the operators is unspecified.