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

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

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

問題描述

限時送ChatGPT賬號..

下面函數返回的指針是否有效?

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?

推薦答案

關于存儲時長:

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

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

結合3.7.1閱讀

3.7.1.

所有沒有動態存儲期,沒有線程存儲期,并且是非本地有靜態存儲期限.這些物品的儲存應持續到程序 (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 條:

更改:字符串文字變為 const字符串文字的類型從char 數組"更改為const char 數組".的類型char16_t 字符串文字從某種整數類型的數組"更改為const char16_t 的數組".這char32_t 字符串文字的類型從某個整數類型的數組"更改為const char32_- 的數組"噸."寬字符串字面量的類型從wchar_t 數組"更改為const wchar_t 數組".

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

基本原理:這避免調用不適當的重載函數,該函數可能期望能夠修改它的參數.

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

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

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

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

動態分配(標準中AFAIK內存區域的上下文中永遠不會使用堆"這個詞)內存需要一個函數調用,該函數調用可能早在靜態內存之后的main發生已分配.

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.

這篇關于函數返回后,指向字符串文字的指針是否仍然有效?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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?(取消引用已刪除的指針總是會導致訪問沖突?)
主站蜘蛛池模板: 国产成人精品一区二区三区 | 亚洲精品久久 | 亚洲在线一区 | 欧美日韩国产传媒 | 亚洲国产免费 | 国产精品免费一区二区三区 | 精品一区二区三区在线视频 | 成人免费福利 | 日本久久精品视频 | 视频二区在线观看 | 美国一级毛片a | 亚洲欧美国产毛片在线 | 国产综合久久 | 亚洲精品影院 | 91国内在线观看 | 欧美国产视频一区二区 | а_天堂中文最新版地址 | 亚洲在线电影 | 午夜tv免费观看 | 亚洲精品日韩在线 | 亚洲精品二区 | 色吧久久 | 亚洲电影一区 | 国产精品久久久久久中文字 | 99国产精品视频免费观看一公开 | 亚洲欧美日韩电影 | 精品国产一区二区三区日日嗨 | 一级做a爰片性色毛片 | 午夜三级视频 | 国产精品久久久久久一区二区三区 | 欧美日韩亚洲在线 | 午夜精品久久 | 中文字幕a√ | 深夜福利亚洲 | 国产 日韩 欧美 在线 | 欧美一区二区三 | av在线天堂网| 亚洲成人精品免费 | 久久精品一区二区三区四区 | 成人免费视频网站在线看 | 欧美日韩在线精品 |