临时字符串对象的生命周期

问题描述:

大家好,


有人能告诉我以下代码是否可以保证有效吗?


#include< string&gt ;

#include< iostream>


int main()

{

const char * s = std :: string(" Hello World")。c_str();

std :: cout<< s<< std :: endl;

返回0;

}

我理解C ++标准临时字符串的生命周期
$当$

包含它的表达式结束时,在main()的第一行创建的b $ b对象结束(除非一个对象被初始化为对临时的
的引用)。


如果确实如此,则指向的内存无效,因为

临时字符串被破坏。


我正在研究的项目有很多以上的构造,但是

我无法改变所有这些,除非我非常确定

它会导致不确定的行为。


谢谢,


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

Gabor Drasny写道:
Gabor Drasny wrote:
有人能告诉我以下代码是否可以保证有效吗?


不。

#include< string>
#include< iostream>

int main()
{char /> const char * s = std :: string(" Hello World")。c_str();


这很不寻常。为什么不简单写一下


const char * s =" Hello World";


???

std :: cout<< s<< std :: endl;
返回0;
}

据我了解C ++标准,在main的第一行创建的临时字符串
对象的生命周期( )当包含它的表达式结束时结束(除非将对象初始化为对临时的引用)。


是。

如果这是真的,那么指向的内存无效,因为
临时字符串被破坏了。


它是。

我正在开发的项目有很多以上的构造,但是
我无法改变自己所有这些,除非我非常确定它会导致不确定的行为。
Could anyone tell me if the following code is guaranteed to work or not?
Not.
#include <string>
#include <iostream>

int main()
{
const char* s = std::string("Hello World").c_str();
This is rather unusual. Why not simply write

const char* s = "Hello World";

???
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).
Yes.
If that is true, then the memory s points to is invalidated, since the
temporary string is destructed.
It is.
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.




确实如此。


V



It does.

V


* Gabor Drasny:
* Gabor Drasny:
大家好,

任何人都可以告诉我,以下代码是否可以保证有效或不是?

#include< string>
#include< iostream>

int main()
{char /> const char * s = std :: string(" Hello World")。c_str();
std :: cout<< s<< std :: endl;
返回0;
}


不保证可以工作:


*正式你需要还包括< ostream>。 ;-)


*临时std :: string在表达式后不再存在。


我正在进行的项目有很多很多上面的构造,但是我无法让自己改变所有这些,除非我非常确定它会导致不确定的行为。
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;
}
Not guaranteed to work:

* Formally you need to also include <ostream>. ;-)

* The temporary std::string ceases to exist after the expression.

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.




这是UB。


但请注意


int main()

{

std :: string const& str = std :: string(" Hello,world!");

const char * s = std.c_str();

std :: cout< &LT; s<< std :: endl;

}


不是UB,因为对const的引用保留了临时性,可以这么说。
>
-

答:因为它弄乱了人们通常阅读文字的顺序。

问:为什么这么糟糕?

A:热门发布。

问:usenet和电子邮件中最烦人的事情是什么?



It''s UB.

But note that

int main()
{
std::string const& str = std::string( "Hello, world!" );
const char* s = std.c_str();
std::cout << s << std::endl;
}

is not UB, because that reference to const keeps the temporary, so to speak.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


内联。


" Alf P. Steinbach" &LT;人*** @ start.no&GT;在消息中写道

news:42 **************** @ news.individual.net ...
Inline.

"Alf P. Steinbach" <al***@start.no> wrote in message
news:42****************@news.individual.net...
* Gabor Drasny :
* Gabor Drasny:
大家好,

有人可以告诉我,以下代码是否可以保证有效吗?

#include< string>
#include< iostream>


{zh_cn const char * s = std :: string(" Hello World")。c_str() ;
std :: cout<< s<< std :: endl;
返回0;
}
不保证可以正常工作:

*正式你还需要包含< ostream>。 ;-)

*临时std :: string在表达式后不再存在。

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;
}
Not guaranteed to work:

* Formally you need to also include <ostream>. ;-)

* The temporary std::string ceases to exist after the expression.

我正在进行的项目有很多很多上面的构造,但是我无法让自己改变所有这些,除非我非常确定它会导致不确定的行为。
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.



它' UB。

但请注意

int main()
{
std :: string const& str = std :: string(" Hello,world!");
const char * s = std.c_str();
std :: cout<< s<< std :: endl;
}
不是UB,因为对const的引用保留了临时,所以要



It''s UB.

But note that

int main()
{
std::string const& str = std::string( "Hello, world!" );
const char* s = std.c_str();
std::cout << s << std::endl;
}

is not UB, because that reference to const keeps the temporary, so to



说话。


它可以用Java或其他语言用GC实现,但不能用C ++实现,

这仍然是UB。

-
答:因为它弄乱了人们通常阅读文本的顺序。
问:为什么这么糟糕?
答:热门帖子。
问:usenet和电子邮件中最烦人的事情是什么?


speak.
It might do that in Java or some other language with a GC but not in C++,
this is still UB.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?