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

函數(shù)返回后,指向字符串文字的指針是否仍然有

Do pointers to string literals remain valid after a function returns?(函數(shù)返回后,指向字符串文字的指針是否仍然有效?)
本文介紹了函數(shù)返回后,指向字符串文字的指針是否仍然有效?的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

限時(shí)送ChatGPT賬號(hào)..

下面函數(shù)返回的指針是否有效?

Is the pointer returned by the following function valid?

const char * bool2str( bool flg )
{
    return flg ? "Yes" : "No";
}

它在 Visual C++ 和 g++ 中運(yùn)行良好.C++ 標(biāo)準(zhǔn)對(duì)此有何評(píng)論?

It works well in Visual C++ and g++. What does C++ standard say about this?

推薦答案

關(guān)于存儲(chǔ)時(shí)長(zhǎng):

2.13.4普通字符串文字和 UTF-8 字符串文字也稱為窄字符串文字.一個(gè)箭頭字符串文字的類型為n const char 數(shù)組",其中 n 是如下定義的字符串大小,并且具有靜態(tài)存儲(chǔ)時(shí)長(zhǎng)

2.13.4 Ordinary string literals and UTF-8 string literals are also referred to as narrow string literals. A narrow string literal has type "array of n const char", where n is the size of the string as defined below, and has static storage duration

結(jié)合3.7.1閱讀

3.7.1.

所有沒有動(dòng)態(tài)存儲(chǔ)期,沒有線程存儲(chǔ)期,并且是非本地有靜態(tài)存儲(chǔ)期限.這些物品的儲(chǔ)存應(yīng)持續(xù)到程序 (3.6.2, 3.6.3).

All objects which do not have dynamic storage duration, do not have thread storage duration, and are not local have static storage duration. The storage for these objects shall last for the duration of the program (3.6.2, 3.6.3).

類型:

附件 C

第 2.13.4 條:

更改:字符串文字變?yōu)?const字符串文字的類型從char 數(shù)組"更改為const char 數(shù)組".的類型char16_t 字符串文字從某種整數(shù)類型的數(shù)組"更改為const char16_t 的數(shù)組".這char32_t 字符串文字的類型從某個(gè)整數(shù)類型的數(shù)組"更改為const char32_- 的數(shù)組"噸."寬字符串字面量的類型從wchar_t 數(shù)組"更改為const wchar_t 數(shù)組".

Change: String literals made const The type of a string literal is changed from "array of char " to "array of const char." The type of a char16_t string literal is changed from "array of some-integer-type " to "array of const char16_t." The type of a char32_t string literal is changed from "array of some-integer-type " to "array of const char32_- t." The type of a wide string literal is changed from "array of wchar_t " to "array of const wchar_t."

基本原理:這避免調(diào)用不適當(dāng)?shù)闹剌d函數(shù),該函數(shù)可能期望能夠修改它的參數(shù).

Rationale: This avoids calling an inappropriate overloaded function, which might expect to be able to modify its argument.

對(duì)原始特征的影響: 改變定義好的特征的語義.轉(zhuǎn)換難度:簡(jiǎn)單的句法轉(zhuǎn)換,因?yàn)樽址淖挚梢赞D(zhuǎn)換為字符*;(4.2).最常見的情況由新的但不推薦使用的標(biāo)準(zhǔn)轉(zhuǎn)換處理:char* p = "abc";//在 C 中有效,在 C++ 中不推薦使用字符* q = expr ?"abc" : "de";//在 C 中有效,在 C++ 中無效

Effect on original feature: Change to semantics of well-defined feature. Difficulty of converting: Simple syntactic transformation, because string literals can be converted to char*; (4.2). The most common cases are handled by a new but deprecated standard conversion: char* p = "abc"; // valid in C, deprecated in C++ char* q = expr ? "abc" : "de"; // valid in C, invalid in C++

使用范圍:有正當(dāng)理由將字符串文字視為潛在指針的程序可修改的內(nèi)存可能很少見.

How widely used: Programs that have a legitimate reason to treat string literals as pointers to potentially modifiable memory are probably rare.

動(dòng)態(tài)分配(標(biāo)準(zhǔn)中AFAIK內(nèi)存區(qū)域的上下文中永遠(yuǎn)不會(huì)使用堆"這個(gè)詞)內(nèi)存需要一個(gè)函數(shù)調(diào)用,該函數(shù)調(diào)用可能早在靜態(tài)內(nèi)存之后的main發(fā)生已分配.

Dynamically allocated (the word 'heap' is never used in context of an area of memory AFAIK in the standard) memory requires a function call that can happen as early as main much after the static memory is allocated.

這篇關(guān)于函數(shù)返回后,指向字符串文字的指針是否仍然有效?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

Difference between std::reference_wrapper and simple pointer?(std::reference_wrapper 和簡(jiǎn)單指針的區(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?(為什么我不能對(duì)普通變量進(jìn)行多態(tài)?)
Dereferencing deleted pointers always result in an Access Violation?(取消引用已刪除的指針總是會(huì)導(dǎo)致訪問沖突?)
主站蜘蛛池模板: 色窝| 毛片视频网站 | 黄色一级大片在线免费看产 | 国产一区精品在线 | 国产性生活视频 | av狠狠干| 免费黄色av | 亚洲毛片视频 | 91av视频在线 | 国产欧美精品一区二区色综合 | 免费一级全黄少妇性色生活片 | av片免费| 精品99视频 | 国产精品一区av | 成人三级视频 | 可以免费看av的网站 | 中文字幕理论片 | 欧美伦理一区二区 | 国产91精品在线观看 | 免费高清av | 日韩精品一区二区三区免费视频 | 香蕉视频一区二区三区 | 久久精品6 | 在线观看免费毛片 | 狠狠做深爱婷婷综合一区 | 黄网站免费在线观看 | 免费在线观看www | 亚洲国产精品久久久久久久 | 中文字幕伊人 | 国产日韩欧美视频 | 超碰免费人人 | 国产精品视频网 | 精品国产成人 | 欧美日韩国产激情 | 色影视 | 欧美日韩中文字幕 | 天天爽夜夜爽夜夜爽精品视频 | 日韩精品在线观看视频 | 四虎最新地址 | 国产精品美女久久久久av爽 | 亚洲一区二区中文字幕 |