std :: string - 未定义的行为? [英] std::string - undefined behavior?

查看:106
本文介绍了std :: string - 未定义的行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

两个不同的编译器为以下代码提供不同的输出:


#include< iostream>

#include< string>


int main(无效)

{

std :: string s(" 0124" ;);

s.replace(0,3,s).replace(s.size(),6,s);


std :: cout<< s<< "大小= QUOT; << s.size()<< std :: endl;

返回0;

}

GCC(3.4.4):

0124401244大小= 10

VC7.1:

012401244 size = 9

这两个中哪一个是正确的?或者,也许这是一个未定义的行为

的std :: string?

解决方案

shablool写道:< blockquote class =post_quotes>大家好,
两个不同的编译器为以下代码提供不同的输出:

#include< iostream>
#include< string>

int main(void)
{
std :: string s(" 0124");
s.replace(0,3,s).replace( s.size(),6,s);

std :: cout<< s<< "大小= QUOT; << s.size()<< std :: endl;
返回0;
}

GCC(3.4.4):
0124401244 size = 10

VC7。 1:
012401244 size = 9

两者中哪一个是正确的?或者,这可能是一个未定义的行为
std :: string?




我认为你在这里遇到了未定义的执行顺序。

编译器是免费的,例如,首先执行最右边的大小()

调用,然后从整个替换问题开始。或者它可以首先做

替换,然后调用最右边的尺寸()等。


-

问候,


Ferdi Smit(理学硕士)

电邮: Fe ******** @ cwi.nl

房间:C0.07电话:4229

INS3可视化和3D界面

CWI荷兰阿姆斯特丹


Ferdi Smit写道:

shablool写道:

大家好,
两个不同的编译器为以下代码提供不同的输出:
#include< iostream>
#include< string>

int main (void)
{
std :: string s(" 0124");
s.replace(0,3,s).replace(s.size(),6,s );

std :: cout<< s<< "大小= QUOT; << s.size()<< std :: endl;
返回0;
}

GCC(3.4.4):
0124401244 size = 10

VC7。 1:
012401244 size = 9

两者中哪一个是正确的?或者,这可能是std :: string的未定义行为?



我认为你在这里遇到了未定义的执行顺序。
编译器是免费的,例如,首先执行最右边的size()
调用,然后从整个替换问题开始。或者它可以先做替换,然后调用最右边的大小()等。




我相信执行的顺序只是未指定,而不是未定义。

未定义意味着程序可以执行任何操作 - 它已经基本上停止了执行任何有用操作的
。未指定意味着该程序有几个可能的执行路径可供选择,并且它完全取决于该程序决定采用哪一条路径。


例如,在上面的程序中,编译器可以选择它将评估表达式的

顺序。任何订单都和其他任何订单一样好;所以即使两个程序从相同的源文件编译

产生不同的输出,这两个程序都不正确。有趣的是,

因为C ++程序必须始终产生相同的行为,因为
相同的输入,甚至未指定的行为都是一致的行为。


Greg


Greg写道:


未定义意味着程序可以做任何事情 - 它基本上停止了
执行任何有用的操作。




嗯,这有点极端。未定义意味着C ++语言

定义并没有告诉你程序会做什么。在许多情况下,你的
编译器文档会告诉你它做了什么,或观察和一点点想法会告诉你各种实现实际上做了什么。


-


Pete Becker

Dinkumware,Ltd。( http://www.dinkumware.com


Hi all,
Two distinct compilers give different output for the following code:

#include <iostream>
#include <string>

int main(void)
{
std::string s("0124");
s.replace(0, 3, s).replace(s.size(), 6, s);

std::cout << s << " size=" << s.size() << std::endl;
return 0;
}
GCC(3.4.4):
0124401244 size=10

VC7.1:
012401244 size=9

Which one of the two is correct? Or, maybe this an undefined behavior
of std::string?

解决方案

shablool wrote:

Hi all,
Two distinct compilers give different output for the following code:

#include <iostream>
#include <string>

int main(void)
{
std::string s("0124");
s.replace(0, 3, s).replace(s.size(), 6, s);

std::cout << s << " size=" << s.size() << std::endl;
return 0;
}
GCC(3.4.4):
0124401244 size=10

VC7.1:
012401244 size=9

Which one of the two is correct? Or, maybe this an undefined behavior
of std::string?



I think you''re running into undefined order of execution here. The
compiler is free, for example, to first execute that rightmost size()
call, and then start with the whole replace issue. Or it could first do
the replace and then call the rightmost size() etc.

--
Regards,

Ferdi Smit (M.Sc.)
Email: Fe********@cwi.nl
Room: C0.07 Phone: 4229
INS3 Visualization and 3D Interfaces
CWI Amsterdam, The Netherlands


Ferdi Smit wrote:

shablool wrote:

Hi all,
Two distinct compilers give different output for the following code:

#include <iostream>
#include <string>

int main(void)
{
std::string s("0124");
s.replace(0, 3, s).replace(s.size(), 6, s);

std::cout << s << " size=" << s.size() << std::endl;
return 0;
}
GCC(3.4.4):
0124401244 size=10

VC7.1:
012401244 size=9

Which one of the two is correct? Or, maybe this an undefined behavior
of std::string?



I think you''re running into undefined order of execution here. The
compiler is free, for example, to first execute that rightmost size()
call, and then start with the whole replace issue. Or it could first do
the replace and then call the rightmost size() etc.



I believe the order of execution is merely unspecified, not undefined.
Undefined means the program may do anything - it has essentially ceased
performing any useful operation. Unspecified means that the program has
a choice of several possible execution paths to take, and it''s entirely
up to the program to decide which one to take.

For example, in the above program, the compiler gets to choose the
order it will evaluate the expressions. Any order is as good as any
other; so even though two programs compiled from identical source files
produce different output, neither program is incorrect. Interestingly,
since a C++ program must always produce identical behavior given
identical inputs, even unspecified behavior is consistent behavior.

Greg


Greg wrote:


Undefined means the program may do anything - it has essentially ceased
performing any useful operation.



Well, that''s a bit extreme. Undefined means that the C++ language
definition doesn''t tell you what the program will do. In many cases your
compiler documentation will tell you what it does, or observation and a
little thought will tell you what various implementations actually do.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)


这篇关于std :: string - 未定义的行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆