Temporary and expression behavior

a 夏天 提交于 2019-12-12 17:06:40

问题


Is this well defined behavior?

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

I am not sure. Reasons?


回答1:


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.




回答2:


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.
....




回答3:


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.




回答4:


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.



来源:https://stackoverflow.com/questions/5756832/temporary-and-expression-behavior

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!