返回“c_str”从函数
这是来自我在网上找到的一个小库:
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());
现在我可以从函数中获取我需要的文本。
我有两个问题:
我的理解正确吗?
我后来注意到
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:
Am I understanding this correctly?
I later noticed that
out
(of typestd::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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
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 returnstd::string
.The static storage of
out
doesn't help, because.str()
returns a temporarystd::string
, which is destroyed when the function exits.你是对的,
out
是分配在数据段上的静态变量。但out.str()
是在堆栈上临时分配的。因此,当您执行return out.str().c_str()
时,您将返回一个指向堆栈临时内部数据的指针。请注意,即使字符串不是堆栈变量,c_str
也“仅被授予在下次调用字符串对象的非常量成员函数之前保持不变”。我认为你已经找到了一个合理的解决方法,假设你不能只返回一个字符串。
You're right that
out
is a static variable allocated on the data segment. Butout.str()
is a temporary allocated on the stack. So when you doreturn 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.
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.在
GetHandStateBrief
中,变量out
不需要是静态的。您需要一个显式的静态字符串
来替换在原始调用out.str()
中创建的临时字符串:In
GetHandStateBrief
, variableout
does not need to be static. You need an explicitstatic string
to replace the temporary that was being created in your original call toout.str()
: