std::string::c_str() and temporaries(std::string::c_str() 和临时变量)
问题描述
以下 C++ 代码格式是否正确:
Is the following C++ code well-formed:
void consumer(char const* p)
{
std::printf("%s", p);
}
std::string random_string_generator()
{
// returns a random std::string object
}
consumer(random_string_generator().c_str());
我遇到的问题是,在创建临时 std::string 对象并获取 c_str() 指针后,没有什么可以阻止 std::string 对象被破坏(或者我可能错了?).如果代码没问题的话,你能不能给我指出标准.当我用 g++ 测试时,它确实有效.
The problem I have with it is, that after creating the temporary std::string object and taking the c_str() pointer, nothing prevents the std::string object from getting destroyed (or maybe I'm wrong?). Can you please point me to the standard, if the code is OK despite everything. It does work, when I test with g++.
推荐答案
std::string::c_str()
返回的指针指向内存由字符串对象维护.它一直有效,直到非常量在字符串对象上调用函数,或者字符串对象是毁坏了.您关心的字符串对象是临时对象.它将在完整表达式的末尾销毁,而不是在之前和不是之后.在您的情况下,完整表达式的结尾在调用 consumer
,所以你的代码是安全的.如果 consumer
将指针保存在某处,以备后用.
The pointer returned by std::string::c_str()
points to memory
maintained by the string object. It remains valid until a non-const
function is called on the string object, or the string object is
destructed. The string object you're concerned about is a temporary.
It will be destructed at the end of the full expression, not before and
not after. In your case, the end of the full expression is after the
call to consumer
, so your code is safe. It wouldn't be if consumer
saved the pointer somewhere, with the idea of using it later.
从 C++98 开始就严格定义了临时对象的生命周期.在此之前,它会有所不同,具体取决于编译器和您编写的代码编写的不会与 g++ 一起工作(1995 年之前,粗略地—g++当标准委员会投票时几乎立即改变了这一点).(那时也没有 std::string
,但同样的问题会影响任何用户编写的字符串类.)
The lifetime of temporaries has been strictly defined since C++98.
Before that, it varied, depending on the compiler, and the code you've
written wouldn't have worked with g++ (pre 1995, roughly—g++
changed this almost immediately when the standards committee voted it).
(There wasn't an std::string
then either, but the same issues affect
any user written string class.)
这篇关于std::string::c_str() 和临时变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:std::string::c_str() 和临时变量
基础教程推荐
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 从 std::cin 读取密码 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01