久久久久久久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,

指針的減法只有在兩個指針都指向時才定義同一個數(shù)組的元素(雖然語言沒有快速的方法確保情況如此).當從另一個指針中減去一個指針時,結果是兩個指針之間的數(shù)組元素數(shù)(一個整數(shù)).可以將一個整數(shù)加到一個指針上或減去一個來自指針的整數(shù);在這兩種情況下,結果都是一個指針值.如果該值不指向與原始指針或超出的指針,使用該值的結果是未定義.

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
}

我在維基百科上閱讀了關于未指明行為的內(nèi)容.它說

I was reading about unspecified behavior on Wikipedia. It says that

在 C 和 C++ 中,只有當指針指向同一對象的成員或同一數(shù)組的元素時,才嚴格定義指向?qū)ο蟮闹羔樀谋容^.

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++ 標準對此有何規(guī)定?

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.

推薦答案

注意,指針減法和指針比較是不同的操作,不同的規(guī)則.

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:

除非兩個指針都指向同一個數(shù)組對象的元素或指向數(shù)組對象最后一個元素之后的元素,否則行為未定義.

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:

比較指向?qū)ο蟮闹羔樁x如下:

Comparing pointers to objects is defined as follows:

  • 如果兩個指針指向同一數(shù)組的不同元素或其子對象,則指向下標較高的元素的指針比較大.

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

如果一個指針指向數(shù)組的一個元素或其子對象,而另一個指針指向數(shù)組的最后一個元素,則后一個指針比較大.

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.

如果兩個指針遞歸地指向同一對象的不同非靜態(tài)數(shù)據(jù)成員,或指向此類成員的子對象,則指向后聲明成員的指針比較大,前提是這兩個成員具有相同的訪問控制和前提是他們的班級不是聯(lián)合.

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.

如果兩個操作數(shù) pq 比較相等(5.10),p<=qp>=q 都產(chǎn)生 truepp>q 都產(chǎn)生假.否則,如果指針 p 比較大于指針 qp>=qp>qq<=pq 都產(chǎn)生 truep<=q, pq>=pq>p 都會產(chǎn)生 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模板網(wǎng)!

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

相關文檔推薦

Difference between std::reference_wrapper and simple pointer?(std::reference_wrapper 和簡單指針的區(qū)別?)
Difference between const. pointer and reference?(常量之間的區(qū)別.指針和引用?)
How to access the contents of a vector from a pointer to the vector in C++?(c++ - 如何從指向向量的指針訪問向量的內(nèi)容?)
Meaning of *amp; and **amp; in C++(*amp; 的含義和**amp;在 C++ 中)
Why can#39;t I do polymorphism with normal variables?(為什么我不能對普通變量進行多態(tài)?)
Dereferencing deleted pointers always result in an Access Violation?(取消引用已刪除的指針總是會導致訪問沖突?)
主站蜘蛛池模板: 亚洲人人 | 中文字幕亚洲国产 | 国产精品视频专区 | 久久99蜜桃综合影院免费观看 | 久久久久久国产精品 | 久久久久久电影 | 亚洲精品日韩一区二区电影 | 黄在线免费观看 | 国产久 | 成人国产精品久久 | www.蜜桃av | 亚洲逼院 | 翔田千里一区二区 | 日韩播放| 成人精品国产免费网站 | 国产精品久久久久久久久久久久冷 | 国产免费一区二区三区 | 国产999精品久久久久久 | 一区二区三区视频在线观看 | 久久99精品久久久久久噜噜 | 国产男女视频 | 男女网站视频 | 欧美日韩在线电影 | 欧美8一10sex性hd | 亚洲日日操 | 激情网五月天 | 日本精品网站 | 久久久视频在线 | 亚洲久草 | 日韩aⅴ视频 | 久久综合欧美 | 欧美激情精品久久久久久变态 | 国产亚洲精品久久午夜玫瑰园 | 久久国产精品亚洲 | 伦理午夜电影免费观看 | 亚洲精品久久久久久一区二区 | 久久精品久久久久久 | 999久久久| 99国产精品视频免费观看一公开 | 伊人久久伊人 | 97精品超碰一区二区三区 |