使用未定义的变量 [英] Use an undefinied variable

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

问题描述



我有一个问题。这个语句被g ++编译器接受(v。

4.1.2,在linux下):


char c =''a'';

string s = s + c;


但变量s在定义之前使用!为什么?这是正确的吗?


程序在到达声明时中止。

如果我们将上述代码更改为:

string s = s +''a'';


错误发生变化。引发了分段错误。

有人能告诉我为什么代码被编译器接受了吗?

谢谢


Piero Zanetti

Hi,
I have a question. This statement is accepted by the g++ compiler (v.
4.1.2, under linux):

char c = ''a'';
string s = s + c;

But the variable s is used before being defined! Why? It''s correct?

The program aborts when the statement is reached.
If we change the above code into:

string s = s + ''a'';

the error changes. A segmentation fault is raised.
Can anyone tell me why the code is accepted by the compiler?
Thanks

Piero Zanetti

推荐答案

2008年3月7日星期五01:16:56 -0800,pierozanetti写道:
On Fri, 07 Mar 2008 01:16:56 -0800, pierozanetti wrote:



我有一个问题。这个语句被g ++编译器接受(v。

4.1.2,在linux下):


char c =''a'';

string s = s + c;


但变量s在定义之前使用!为什么?这是正确的?
Hi,
I have a question. This statement is accepted by the g++ compiler (v.
4.1.2, under linux):

char c = ''a'';
string s = s + c;

But the variable s is used before being defined! Why? It''s correct?



它是有效的代码,但是调用了未定义的行为(实际上我有一点感觉很奇怪g ++没有发出一个警告,即使使用-std = c ++ 98 -pedantic -

Wall)。

It''s valid code, but invokes undefined behaviour (actually I was a little
surprised that g++ doesn''t emit a warning even with -std=c++98 -pedantic -
Wall).


程序在达到语句时中止。如果我们将上面的

代码更改为:


string s = s +''a'';


错误发生了变化。引发了分段错误。
The program aborts when the statement is reached. If we change the above
code into:

string s = s + ''a'';

the error changes. A segmentation fault is raised.



仍未定义的行为。

Still undefined behaviour.


任何人都可以告诉我

为什么代码被编译器接受?
Can anyone tell me
why the code is accepted by the compiler?



因为它是有效的代码。


-

Lionel B

Because it''s valid code.

--
Lionel B


On Fri,2008年3月7日02:35:38 -0800,pierozanetti写道:
On Fri, 07 Mar 2008 02:35:38 -0800, pierozanetti wrote:

3月7日, 11:14,Lionel B< m ... @ privacy.netwrote:
On 7 Mar, 11:14, Lionel B <m...@privacy.netwrote:

> On Fri,2008年3月7日01:16:56 -0800,pierozanetti写道:
>On Fri, 07 Mar 2008 01:16:56 -0800, pierozanetti wrote:



我有一个问题。这个语句被g ++编译器接受(v。

4.1.2,在linux下):
Hi,
I have a question. This statement is accepted by the g++ compiler (v.
4.1.2, under linux):


char c = ''a'';

string s = s + c;
char c = ''a'';
string s = s + c;


但变量s在定义之前使用!为什么?这是正确的?
But the variable s is used before being defined! Why? It''s correct?


这是有效的代码,但调用了未定义的行为(实际上我很惊讶即使使用-std = c +,g ++也不会发出警告+98
-pedantic - Wall)。


It''s valid code, but invokes undefined behaviour (actually I was a
little surprised that g++ doesn''t emit a warning even with -std=c++98
-pedantic - Wall).


程序在到达语句时中止。如果我们将以上代码更改为:
The program aborts when the statement is reached. If we change the
above code into:


string s = s +''a'';
string s = s + ''a'';


错误更改。引发了分段错误。
the error changes. A segmentation fault is raised.


仍未定义的行为。


Still undefined behaviour.


任何人都可以告诉我

为什么代码被接受编译器?
Can anyone tell me
why the code is accepted by the compiler?


因为它是有效的代码。


Because it''s valid code.



使用未定义的变量是一个有效的代码?!?


Use an undefined variable is a valid code?!?



是的。说清楚:


有效 ==符合C ++标准

" valid" !=做一些明智的事情


例如这是有效的代码:


int main()

{

int x = 0/0;

}


当然,这是非常愚蠢的代码(我的编译器会发出一个问题)几个

警告)但它编译。<​​br />

-

Lionel B

Yes. To make it clear:

"valid" == complies with the C++ standard
"valid" != does something sensible

For instance this is valid code:

int main()
{
int x = 0/0;
}

Sure, it''s extremely silly code (and my compiler issues a couple of
warnings) but it compiles.

--
Lionel B


pierozanetti写道:
pierozanetti wrote:

使用未定义的变量是一个有效的代码?!?
Use an undefined variable is a valid code?!?



他的意思是格式良好。也就是说,编译器不需要

来检测并发出诊断信息。


He means "well formed." That is, the compiler is not required
to detect and issue a diagnositc for it.


这篇关于使用未定义的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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