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

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

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

問題描述

限時送ChatGPT賬號..

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

Is the pointer returned by the following function valid?

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

它在 Visual C++ 和 g++ 中運行良好.C++ 標準對此有何評論?

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

推薦答案

關(guān)于存儲時長:

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

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.

所有沒有動態(tài)存儲期,沒有線程存儲期,并且是非本地有靜態(tài)存儲期限.這些物品的儲存應持續(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 字符串文字的類型從某個整數(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)用不適當?shù)闹剌d函數(shù),該函數(shù)可能期望能夠修改它的參數(shù).

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

對原始特征的影響: 改變定義好的特征的語義.轉(zhuǎn)換難度:簡單的句法轉(zhuǎn)換,因為字符串文字可以轉(zhuǎn)換為字符*;(4.2).最常見的情況由新的但不推薦使用的標準轉(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++

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

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

動態(tài)分配(標準中AFAIK內(nèi)存區(qū)域的上下文中永遠不會使用堆"這個詞)內(nèi)存需要一個函數(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ù)返回后,指向字符串文字的指針是否仍然有效?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guā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?(取消引用已刪除的指針總是會導致訪問沖突?)
主站蜘蛛池模板: jlzzjlzz欧美大全 | 久久看精品 | 日韩精品一区二区三区在线观看 | 欧美成人激情 | 久久精品综合网 | 欧美日韩精品久久久免费观看 | 91xxx在线观看| 成人黄色电影在线观看 | 国产99免费| 国产精品高清在线 | 欧美综合一区二区三区 | 国产精品69久久久久水密桃 | 国产高清美女一级a毛片久久w | 亚洲欧洲成人 | 九九热精品视频 | 91网站在线看| 在线欧美视频 | 亚洲欧美精品久久 | 成人欧美一区二区 | 日韩精品一区二区不卡 | 黄色一级特级片 | 欧美精品一区二区三区四区五区 | 国产日韩视频 | 成人欧美日韩一区二区三区 | 99精品欧美一区二区蜜桃免费 | 一区二区小视频 | 日韩免费看片 | 国产日韩久久 | 欧美色综合天天久久综合精品 | 黑人精品欧美一区二区蜜桃 | 99久久久久久 | 成人精品一区二区三区 | 成人免费一区二区 | 欧美久久精品一级c片 | 曰韩一二三区 | 日韩精品一区二区三区视频播放 | 成人在线免费 | 日本高清视频在线播放 | 国产精品自产av一区二区三区 | 久久6视频 | 国产精品美女久久久久久免费 |