久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

C++ 中的指針比較是未定義或未指定的行為嗎?

Is pointer comparison undefined or unspecified behavior in C++?(C++ 中的指針比較是未定義或未指定的行為嗎?)
本文介紹了C++ 中的指針比較是未定義或未指定的行為嗎?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

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.

如果兩個操作數 pq 比較相等(5.10),p<=qp>=q 都產生 truepp>q 都產生假.否則,如果指針 p 比較大于指針 qp>=qp>qq<=pq 都產生 truep<=q, pq>=pq>p 都會產生 false.否則,每個運算符的結果都是未指定的.

If two operands 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.

這篇關于C++ 中的指針比較是未定義或未指定的行為嗎?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Difference between std::reference_wrapper and simple pointer?(std::reference_wrapper 和簡單指針的區別?)
Difference between const. pointer and reference?(常量之間的區別.指針和引用?)
How to access the contents of a vector from a pointer to the vector in C++?(c++ - 如何從指向向量的指針訪問向量的內容?)
Meaning of *amp; and **amp; in C++(*amp; 的含義和**amp;在 C++ 中)
Why can#39;t I do polymorphism with normal variables?(為什么我不能對普通變量進行多態?)
Dereferencing deleted pointers always result in an Access Violation?(取消引用已刪除的指針總是會導致訪問沖突?)
主站蜘蛛池模板: 成人在线网址 | 欧美激情一区 | 亚洲天堂网址 | 欧美精品一级片 | 日本不卡在线 | 午夜国产在线观看 | 亚洲视频精品 | 99国产免费 | 天天干网站 | 国产在线黄色 | 91性高潮久久久久久久久 | 特级淫片裸体免费看 | 91色国产| 国产免费成人 | 久操国产 | 国产乱码一区二区 | 午夜欧美| 亚洲免费精品 | 日韩国产精品一区二区 | 岛国精品在线播放 | 国产网站免费 | 欧美日韩亚洲一区 | a级一级片| 国产又粗又猛又黄又爽无遮挡 | 少妇高潮露脸国语对白 | 91久久国产综合久久91精品网站 | 成年人视频在线播放 | 久久视频一区二区 | 成人av一区| 一级欧美一级日韩 | 午夜在线播放 | 在线观看福利影院 | 日韩一区二区三区在线播放 | 美女无遮挡网站 | 国产高清视频在线 | 夜夜操夜夜操 | 国产a毛片| 伊人精品久久 | 成人精品在线视频 | 黄色av大片 | 国产精品欧美在线 |