G
Gabor Drasny
Hi all,
Could anyone tell me if the following code is guaranteed to work or not?
#include <string>
#include <iostream>
int main()
{
const char* s = std::string("Hello World").c_str();
std::cout << s << std::endl;
return 0;
}
As I understand the C++ standard the lifetime of the temporary string
object created on the first line of main() ends when the expression
containing it ends (unless an object is initialized as a reference to
the temporary).
If that is true, then the memory s points to is invalidated, since the
temporary string is destructed.
The project I am working on has quite a lot of the above construct, but
I could not bring myself to change all of these, unless I am quite sure
it causes undefined behavior.
Thanks,
Gabor Drasny
Could anyone tell me if the following code is guaranteed to work or not?
#include <string>
#include <iostream>
int main()
{
const char* s = std::string("Hello World").c_str();
std::cout << s << std::endl;
return 0;
}
As I understand the C++ standard the lifetime of the temporary string
object created on the first line of main() ends when the expression
containing it ends (unless an object is initialized as a reference to
the temporary).
If that is true, then the memory s points to is invalidated, since the
temporary string is destructed.
The project I am working on has quite a lot of the above construct, but
I could not bring myself to change all of these, unless I am quite sure
it causes undefined behavior.
Thanks,
Gabor Drasny