的(串)文字范围 [英] Scope of (string) literals

查看:115
本文介绍了的(串)文字范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我总是尽量避免返回字符串,因为我怕他们不是函数外定义。但我不知道,如果是这种情况。让我们举个例子来说,这个功能:

I always try to avoid to return string literals, because I fear they aren't defined outside of the function. But I'm not sure if this is the case. Let's take, for example, this function:


const char *
return_a_string(void)
{
    return "blah";
}

这是正确的code?它不为我工作,但也许它仅适用于我的编译器(GCC)。所以,问题是,(串)文字有一个范围,或者是他们present /中定义的所有的时间。

Is this correct code? It does work for me, but maybe it only works for my compiler (gcc). So the question is, do (string) literals have a scope or are they present/defined all the time.

推荐答案

这code是跨平台的罚款。该字符串被编译成二进制为静态字符串。如果你是在例如窗户,你甚至可以用记事本打开你的.exe和搜索字符串本身。

This code is fine across all platforms. The string gets compiled into the binary as a static string literal. If you are on windows for example you can even open your .exe with notepad and search for the string itself.

由于它是一个静态的字符串范围并不重要。

Since it is a static string literal scope does not matter.

字符串池:

一件事看出来的是,在某些情况下,相同的字符串可以被合并,以节省空间中的可执行文件。在这种情况下,每个字符串,这是同样可以有相同的内存地址。你不应该认为它会或不会出现这种情况,虽然。

One thing to look out for is that in some cases, identical string literals can be "pooled" to save space in the executable file. In this case each string literal that was the same could have the same memory address. You should never assume that it will or will not be the case though.

在大多数编译器可以设置是否使用静态字符串池为stirng文字。

In most compilers you can set whether or not to use static string pooling for stirng literals.

字符串的最大尺寸:

若干编译器都为字符串的最大尺寸。例如用VC ++这是大约2048字节。

Several compilers have a maximum size for the string literal. For example with VC++ this is approximately 2,048 bytes.

修改字符串给出未定义的行为:

修改字符串不应该做。它有一个未定义的行为。

Modifying a string literal should never be done. It has an undefined behavior.

char * sz = "this is a test";
sz[0] = 'T'; //<--- undefined results

宽字符串文字:

以上所有同样适用于宽字符串文字。

All of the above applies equally to wide string literals.

例如:L这是一个宽字符串文字;

Example: L"this is a wide string literal";

C ++标准规定:(第lex.string)

The C++ standard states: (section lex.string)

1字符串文字是一个序列
  字符(如在定义的
  的 lex.ccon 的),用双引号包围,可选地开始
  字母L,如...或L...。字符串文字不开始
  与L是一个普通的字符串,也称作窄
  字符串字面量。一个普通的字符串字面量的类型是N数组
  常量
  字符和静态存储持续时间( basic.stc 的),其中n是
  尺寸
  如下面所定义,并且与给定的初始化的字符串
  字符。字符串文字即以L开头,如LASDF
  是
  宽字符串文字。宽字符串文字的类型的数组
  ñ
  常量为wchar_t,并具有静态存储持续时间,其中n是大小
  的
  如下面所定义,并且与给定的字符初始化串
  字符。

1 A string literal is a sequence of characters (as defined in lex.ccon) surrounded by double quotes, optionally beginning with the letter L, as in "..." or L"...". A string literal that does not begin with L is an ordinary string literal, also referred to as a narrow string literal. An ordinary string literal has type "array of n const char" and static storage duration (basic.stc), where n is the size of the string as defined below, and is initialized with the given characters. A string literal that begins with L, such as L"asdf", is a wide string literal. A wide string literal has type "array of n const wchar_t" and has static storage duration, where n is the size of the string as defined below, and is initialized with the given charac- ters.

2,是否所有的字符串文字是不同的(即存储在
    不重叠的对象)是实现定义的。效果
  的
   试图修改字符串是不确定的。

2 Whether all string literals are distinct (that is, are stored in nonoverlapping objects) is implementation-defined. The effect of attempting to modify a string literal is undefined.

这篇关于的(串)文字范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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