返回“c_str”从函数

发布于 2024-08-29 09:12:22 字数 742 浏览 7 评论 0原文

这是来自我在网上找到的一个小库:

const char* GetHandStateBrief(const PostFlopState* state)
{
    static std::ostringstream out;

    // ... rest of the function ...

    return out.str().c_str()
}

在我的代码中,我正在这样做:

const char *d = GetHandStateBrief(&post);
std::cout<< d << std::endl;

现在,首先 d 包含垃圾。然后我意识到,当函数返回时,我从函数获取的 C 字符串会被销毁,因为 std::ostringstream 是在堆栈上分配的。所以我补充道:

return strdup( out.str().c_str());

现在我可以从函数中获取我需要的文本。

我有两个问题:

  1. 我的理解正确吗?

  2. 我后来注意到 out (类型为 std::ostringstream)是用静态存储分配的。这不是意味着该对象应该保留在内存中直到程序终止吗?如果是这样,那么为什么不能访问该字符串?

This is from a small library that I found online:

const char* GetHandStateBrief(const PostFlopState* state)
{
    static std::ostringstream out;

    // ... rest of the function ...

    return out.str().c_str()
}

In my code I am doing this:

const char *d = GetHandStateBrief(&post);
std::cout<< d << std::endl;

Now, at first d contained garbage. I then realized that the C string I am getting from the function is destroyed when the function returns because std::ostringstream is allocated on the stack. So I added:

return strdup( out.str().c_str());

And now I can get the text I need from the function.

I have two questions:

  1. Am I understanding this correctly?

  2. I later noticed that out (of type std::ostringstream) was allocated with static storage. Doesn't that mean that the object is supposed to stay in memory until the program terminates? And if so, then why can't the string be accessed?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

倾城°AllureLove 2024-09-05 09:12:22

strdup 在堆上分配字符串的副本,您必须稍后手动释放它(我认为使用 free() )。如果您可以选择,返回 std::string 会更好。

out 的静态存储没有帮助,因为 .str() 返回一个临时的 std::string,当函数调用时它会被销毁退出。

strdup allocates a copy of the string on the heap, which you have to free manually later (with free() I think). If you have the option, it would be much better to return std::string.

The static storage of out doesn't help, because .str() returns a temporary std::string, which is destroyed when the function exits.

浅忆流年 2024-09-05 09:12:22

你是对的,out 是分配在数据段上的静态变量。但 out.str() 是在堆栈上临时分配的。因此,当您执行 return out.str().c_str() 时,您将返回一个指向堆栈临时内部数据的指针。请注意,即使字符串不是堆栈变量,c_str 也“仅被授予在下次调用字符串对象的非常量成员函数之前保持不变”。

我认为你已经找到了一个合理的解决方法,假设你不能只返回一个字符串。

You're right that out is a static variable allocated on the data segment. But out.str() is a temporary allocated on the stack. So when you do return out.str().c_str() you're returning a pointer to a stack temporary's internal data. Note that even if a string is not a stack variable, c_str is "only granted to remain unchanged until the next call to a non-constant member function of the string object."

I think you've hit on a reasonable workaround, assuming you can't just return a string.

心在旅行 2024-09-05 09:12:22

strdup() 返回一个指向堆上内存的 char* 指针。当你完成它时,你需要 free() 它,但是,是的,这会起作用。

在这种情况下,静态局部变量 std::ostringstream out 没有任何意义,除非返回的 std::string 也是静态的,而您的观察显示这是不正确的。

strdup() returns a char* pointer that is pointing to memory on the heap. You need to free() it when you're done with it, but yes, that will work.

The static local variable std::ostringstream out makes no sense in this case, unless the std::string being returned was also static which your observation is showing to be not true.

天荒地未老 2024-09-05 09:12:22

GetHandStateBrief 中,变量 out 不需要是静态的。您需要一个显式的静态字符串来替换在原始调用out.str()中创建的临时字符串:

static std::string outStr;
std::ostringstream out;
... rest of function ...
outStr = out.str();
return outStr.c_str();

In GetHandStateBrief, variable out does not need to be static. You need an explicit static string to replace the temporary that was being created in your original call to out.str():

static std::string outStr;
std::ostringstream out;
... rest of function ...
outStr = out.str();
return outStr.c_str();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文