指向字符串字面的指针在函数返回后仍然有效吗? [英] Do pointers to string literals remain valid after a function returns?

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

问题描述

以下函数返回的指针是否有效?

  const char * bool2str(bool flg)
{
return flg? 是:否;
}

它在Visual C ++和g ++中运行良好。

$ p









$ b < blockquote>

2.13.4
普通字符串文字和UTF-8字符串文字也称为窄字符串文字。一个窄的
字符串文字具有类型array of n const char,其中n是下面定义的字符串的大小,并且有
静态存储持续时间




与3.7.1一起阅读


strong>



没有动态存储持续时间,没有线程存储持续时间,以及
不在本地的所有对象都有静态存储持续时间。这些对象的存储将持续
程序(3.6.2,3.6.3)的持续时间。


在类型上:


附件C



strong>子条款2.13.4:



更改:字符串常量const
字符串文字的类型是从char数组更改为const char数组。
char16_t字符串文本的类型从一些整数类型的数组更改为const char16_t的数组
类型的char32_t字符串文字从一些整数类型的数组更改为const char32_-
t的数组。宽字符串文字的类型从wchar_t数组更改为数组const wchar_t。



原理:这避免调用不当的重载函数,可能期望能够
修改



对原始特征的影响:更改为定义良好的特征的语义。
转换的困难:简单的语法转换,因为字符串文字可以转换为
char *; (4.2)。最常见的情况是通过一个新的,但已过时的标准转换处理:
char * p =abc; // valid in C,deprecated in C ++
char * q = expr? abc:de; //在C中有效,在C ++中无效



使用范围:有正当理由将字符串文字当作指针的程序b $ b可修改的内存可能很少。


动态分配AFAIK在标准)内存需要一个函数调用,可以发生早在 main 分配静态内存后。


Is the pointer returned by the following function valid?

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

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

解决方案

On storage duration:

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

read in conjunction with 3.7.1

3.7.1.

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

On type:

Annex C

Subclause 2.13.4:

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.

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.

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.

这篇关于指向字符串字面的指针在函数返回后仍然有效吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆