简体   繁体   English

在堆上分配C样式的字符串

[英]Allocating C-style string on the Heap

guys, I need a little technical help. 伙计们,我需要一点技术帮助。 I'm working in C++, don't have much experience working in it but know the language somewhat. 我在C ++中工作,没有太多的工作经验,但是对语言有所了解。 I need to use a C-style string (char array) but I need to allocate it on the heap. 我需要使用C样式的字符串(char数组),但需要在堆上分配它。

If you look at this very simple piece of code: 如果您看这段非常简单的代码:

#include <iostream>
using namespace std;

char* getText()
{
    return "Hello";
}

int main()
{
    char* text;
    text = getText();
    cout << text;
    //delete text; // Calling delete results in an error
}

Now, I'm assuming that the "Hello" string is allocated on the stack, within getText(), which means the pointer will be "floating" as soon as getText returns, am I right? 现在,我假设将“ Hello”字符串分配在堆栈中的getText()中,这意味着一旦getText返回,指针就会“浮动”,对吗?

If I'm right, then what's the best way to put "Hello" on the heap so I can use that string outside of getText and call delete on the pointer if I need to? 如果我是对的,那么将“ Hello”放在堆上的最佳方法是什么,这样我可以在getText之外使用该字符串,并在需要时在指针上调用delete?

No, there's no hidden stack allocation going on there. 不,这里没有隐藏的堆栈分配。 "Hello" is static data, and ends up in the .data segment of your program. "Hello"是静态数据,最终出现在程序的.data段中。

This also means the string is shared for all calls to getText . 这也意味着该字符串对于所有对getText调用都是共享的。 A common use when this would be acceptable is if you have a large list of error messages that map to error codes. 可接受的一种常见用法是,如果您有大量映射到错误代码的错误消息。 Functions like strerror work like this, so that you can get descriptive error messages for standard library error codes. strerror这样的函数就是这样工作的,因此您可以获得标准库错误代码的描述性错误消息。 But nobody is supposed to modify the return value of strerror (also because it is const ). 但是没有人应该修改strerror的返回值(也因为它是const )。 In your case, your function definition should read: 在您的情况下,您的函数定义应为:

const char *getText()

If you do want a private copy of the string returned, you can use the strdup function to make a copy: 如果确实希望返回字符串的私有副本,则可以使用strdup函数进行复制:

return strdup("Hello");

This is not right. 这是不对的。 "Hello" is a static string constant and it really should be const char*. “ Hello”是一个静态字符串常量,它实际上应该是const char *。

Use a std::string , from the <string> header. 使用<string>标头中的std::string Then use its .c_str() member function. 然后使用其.c_str()成员函数。 Then you don't have to care about allocation and deallocation: it takes care of it for you, correctly. 然后,您不必在乎分配和释放:它会为您正确地处理它。

Cheers & hth., 干杯&hth。,

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 . 窄字符串文字的类型为“ n of const char数组”,其中n是下面定义的字符串的大小,并且具有静态存储期限

Static storage is neither automatic ("on the stack") nor dynamic ("on the heap"). 静态存储既不是自动的 (“在堆栈上”)也不是动态的 (“在堆上”)。 It is allocated prior to the actual runtime of your program, so pointers to string literals never become invalid. 它是在程序实际运行之前分配的,因此指向字符串文字的指针永远不会变为无效。

Note that char* p = "Hello" is deprecated because it is dangerous: the type system cannot prevent you from trying to change the string literal through p (which would result in undefined behavior ). 请注意,不建议使用char* p = "Hello" ,因为这样做很危险:类型系统无法阻止您尝试通过p更改字符串文字(这将导致未定义的行为 )。 Use const char* p = "Hello" instead. 请改用const char* p = "Hello"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM