如何安全可靠的C ++字符串字面量? [英] How safe and reliable are C++ String Literals?

查看:94
本文介绍了如何安全可靠的C ++字符串字面量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我想要更好地了解C ++中的字符串字面量如何工作。我主要关心的情况下,你将一个字符串字面量的地址分配给指针,并传递它。例如:

So, I'm wanting to get a better grasp on how string literals in C++ work. I'm mostly concerned with situations where you're assigning the address of a string literal to a pointer, and passing it around. For example:

char* advice = "Don't stick your hands in the toaster.";

现在让我们说,我只是通过复制指针在程序的持续时间传递这个字符串。当然,这可能不是一个好主意,但我很好奇在幕后将会发生什么。

Now lets say I just pass this string around by copying pointers for the duration of the program. Sure, it's probably not a good idea, but I'm curious what would actually be going on behind the scenes.

另一个例子,假设我们创建一个返回字符串字面量的函数:

For another example, let's say we make a function that returns a string literal:

char* foo()
{
    // function does does stuff
    return "Yikes!"; // somebody's feeble attempt at an error message
}

现在让我们说这个函数被调用非常经常,字符串字面值只使用大约一半的时间它被调用:

Now lets say this function is called very often, and the string literal is only used about half the time it's called:

// situation #1: it's just randomly called without heed to the return value
foo(); 

// situation #2: the returned string is kept and used for who knows how long
char* retVal = foo();

在第一种情况下,实际发生了什么?是字符串刚刚创建,但没有使用,永远不会释放?

In the first situation, what's actually happening? Is the string just created but not used, and never deallocated?

在第二种情况下,是否只要用户发现需要它就要维护的字符串?当不再需要时,会发生什么...那个内存会被释放(假设没有指向那个空间)?

In the second situation, is the string going to be maintained as long as the user finds need for it? What happens when it isn't needed anymore... will that memory be freed up then (assuming nothing points to that space anymore)?

不要误会我,我不打算使用这样的字符串文字。我计划使用一个容器来保持我的字符串检查(可能std :: string)。我大多只是想知道这些情况是否会导致内存管理或数据损坏的问题。

Don't get me wrong, I'm not planning on using string literals like this. I'm planning on using a container to keep my strings in check (probably std::string). I'm mostly just wanting to know if these situations could cause problems either for memory management or corrupted data.

推荐答案

字符串文字类型 const char [N] 其中 N 是长度+1),并且是静态分配的。你不必担心内存问题;如果在你的程序中使用了一个字符串,它会为你处理,并驻留在程序存储器的某个地方(通常只读)。

String-literals have the type const char[N] (where N is the length + 1) and are statically allocated. You need not worry about memory issues; if a string is used in your program it is all handled for you, and resides somewhere in program memory (usually read-only).

same:

static const char str[] = "a string";
"a string"

指向字符串文字时,数组中的第一个字符。事实上,因为类型是 const char [] ,所以只能通过 const char * 来安全地指向它。从字符串字面值到 char * 的转换已弃用,并且不安全。

When you point to a string literal, you are pointing to the first character at the array. In fact, because the type is const char[], it's only safe to point to it via const char*. The conversion from string literal to char* is deprecated, and unsafe.

// the "same"
static const char str[] = "a string";
const char* strPtr = str; // decays

const char* s1 = "a string";
char* s2 = "a string"; // allowed, implicit const_cast

*s1 = 'A'; // not allowed, it's const
*s2 = 'B'; // allowed, it's not const (but leads to undefined behavior)

这篇关于如何安全可靠的C ++字符串字面量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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