临时和表达行为

发布于 2024-11-03 01:28:27 字数 154 浏览 0 评论 0原文

这是明确定义的行为吗?

const char* p = (std::string("Hello") + std::string("World")).c_str();
std::cout << p;

我不知道。原因?

Is this well defined behavior?

const char* p = (std::string("Hello") + std::string("World")).c_str();
std::cout << p;

I am not sure. Reasons?

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

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

发布评论

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

评论(4

难忘№最初的完美 2024-11-10 01:28:27

不,这是未定义的行为。 std::string 临时变量和 operator+ 返回的临时变量都只能存活到 const char* 初始化结束(完整的结束)表达)。然后它们被销毁并且p指向不确定的内存。

No, this is undefined behavior. Both std::string temporaries and the temporary returned by operator+ only live until the end of the initialization of your const char* (end of full expression). Then they are destroyed and p points to uncertain memory.

Smile简单爱 2024-11-10 01:28:27

否,行为未定义,因为 p 指向 std::cout << 中已释放的存储空间。 p;

创建一个临时对象来保存 std::string("Hello") + std::string("World")。然后从该对象中检索 C 样式字符串。在表达式末尾,临时对象被销毁,留下 p 指向已释放的存储空间。

然后使用 p 调用未定义的行为。

12.2/4 说

在两种情况下,临时变量会在与完整表达式结束时不同的点被销毁。第一个上下文是当表达式作为定义对象的声明符的初始值设定项出现时。在这种情况下,保存表达式结果的临时变量将持续存在,直到对象的
初始化完成。
....

No the behaviour is undefined because p points to deallocated storage in std::cout << p;

A temporary is created to hold std::string("Hello") + std::string("World"). C-style string is then retrived from that object. At the end of the expression that temporary is destroyed leaving p pointing to a deallocated storage.

Using p then invokes Undefined Behavior.

12.2/4 says

There are two contexts in which temporaries are destroyed at a different point than the end of the full-expression. The first context is when an expression appears as an initializer for a declarator defining an object. In that context, the temporary that holds the result of the expression shall persist until the object’s
initialization is complete.
....

我们的影子 2024-11-10 01:28:27

由于缺少分号,它不会编译:

const char* p = (std::string("Hello") + std::string("World")).c_str(); //<< important here
std::cout << p;

现在规则适用,临时值在其使用的表达式末尾(分号处)被删除。所以你有一个指向已删除内存的指针,这会导致未定义的行为。

it won't compile because of a missing semi-colon:

const char* p = (std::string("Hello") + std::string("World")).c_str(); //<< important here
std::cout << p;

NOW the rule applies that the temporary is deleted at the end of the expression it is used in, which is at the semicolon. So you have a pointer to deleted memory which causes undefined behaviour.

咿呀咿呀哟 2024-11-10 01:28:27

我最近读了这本优秀的书 http://www.amazon.co.uk/Gotchas-Avoiding-Addison-Wesley-Professional-Computing/dp/0321125185/ref 如果我没记错的话,这几乎是给出的示例之一那里。

我相信从 c_str 返回的数据仅在返回它的字符串对象有效时才有效。示例中的字符串对象仅在表达式的持续时间内有效。

I recently read this excellent book http://www.amazon.co.uk/Gotchas-Avoiding-Addison-Wesley-Professional-Computing/dp/0321125185/ref and if I recall correctly this is pretty much one of the examples given there.

I believe the data returned from c_str is only valid as long as the string object that returned it is live. The string objects in your example are only live for the duration of the expression.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文