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

时间:2023-04-26
本文介绍了函数返回后,指向字符串文字的指针是否仍然有效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送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模板网!

上一篇:c++删除指针问题,仍然可以访问数据 下一篇:对于具有线性存储的容器,可以使用原始指针代

相关文章

最新文章