编译器的自信心 [英] self-confidence of compiler

查看:69
本文介绍了编译器的自信心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

fDeleted = false;

uint jobId;


foreach(结构中的struct struct){

if(struct。 type == JOB){

jobId = struct.id;

if(struct.dataType == STATUS)

fDeteted =(struct。 data& STATUS_DELETED)!= 0;

}


if(fDeleted)

writeln(jobId +"已完成) ;);


请注意,在''删除''标志获得设置为真的任何

机会之前,初始化作业ID。因此不可能使用未初始化的jobId。

尽管如此,compler强迫我进行无用的初始化。

聪明机器人的设计师是否会因为b
施加的限制而克服这些限制?

解决方案

您的代码不保证将分配jobId。


-

HTH,


Kevin Spencer

Microsoft MVP


打印组件,电子邮件组件,

FTP客户端类,增强型数据控件,更多。

DSI PrintManager,Miradyne组件库:
http: //www.miradyne.net


" valentin tihomirov" < no ************** @ yandex.ruwrote in message

news:OP ************* @ TK2MSFTNGP02。 phx.gbl ...


fDeleted = false;

uint jobId;


foreach (结构中的struct struct){

if(struct.type == JOB){

jobId = struct.id;

if(struct .dataType == STATUS)

fDeteted =(struct.data& STATUS_DELETED)!= 0;

}


if (fDeleted)

writeln(jobId +已完成);


请注意,在删除标志获得之前,工作ID已初始化任何

机会设置为真。因此不可能使用未初始化的jobId。

尽管如此,compler强迫我进行无用的初始化。

设计师的''聪明''机器人是否有任何克服限制

他们强加的?



fDeleted = false;


uint jobId;

foreach(结构中的struct struct){

if(struct.type == JOB){

jobId = struct.id;

if(struct.dataType == STATUS)

fDeteted =(struct.data& STATUS_DELETED)!= 0;

}

if(fDeleted)

writeln(jobId +"已完成);

请注意,在''deleted''标志获得设置为true的任何

机会之前,初始化作业ID。所以不可能使用未初始化的

jobId。然而,compler强迫我做无用的初始化。 聪明机器人的设计师是否会因为他们施加的限制而克服任何问题?



如果结构如何?是空的,foreach中的代码从未运行过吗?


Hans Kesting


" valentin tihomirov" < no ************** @ yandex.ruschrieb im Newsbeitrag

新闻:OP ************* @ TK2MSFTNGP02。 phx.gbl ...


fDeleted = false;

uint jobId;


foreach (结构中的struct struct){

if(struct.type == JOB){

jobId = struct.id;

if(struct .dataType == STATUS)

fDeteted =(struct.data& STATUS_DELETED)!= 0;

}


if (fDeleted)

writeln(jobId +已完成);


请注意,在删除标志获得之前,工作ID已初始化任何

机会设置为真。因此不可能使用未初始化的jobId。

尽管如此,compler强迫我进行无用的初始化。

设计师的''聪明''机器人是否能够克服他们施加的限制




嗨Valentin,


我不确定,你到底在想什么。

当然,这是一个很少的情况,其中一个变量不能被使用

未分配,但编译器认为它可以。这似乎不是C#的特殊问题,但对于任何语言,都禁止使用

未分配的变量。 (虽然可能C#是迄今为止唯一的一个,但我不是肯定的。)


为什么不把它初始化为零?性能考虑因素?


如果你想要一种允许使用未指定变量的语言,你需要使用另一种语言。
< br $>
Christof


PS你可以尝试使用


不安全{uint * p =& jobId;} <循环之前的
。编译器可能会优化这项任务



fDeleted = false;
uint jobId;

foreach (Struct struct in structures) {
if (struct.type == JOB) {
jobId = struct.id;
if (struct.dataType == STATUS)
fDeteted = (struct.data & STATUS_DELETED) != 0;
}

if (fDeleted)
writeln(jobId + " has completed");

Note that the job id is initialized before the ''deleted'' flag gets any
chance to set true. So it is not possible to use not initialized jobId.
Nevertheless, compler forces me to do the useless initialization. Do the
designers of ''clever'' robots forsee any overcoming for the limitations they
impose?

解决方案

Your code does not guarantee that jobId will ever be assigned.

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net

"valentin tihomirov" <no**************@yandex.ruwrote in message
news:OP*************@TK2MSFTNGP02.phx.gbl...

fDeleted = false;
uint jobId;

foreach (Struct struct in structures) {
if (struct.type == JOB) {
jobId = struct.id;
if (struct.dataType == STATUS)
fDeteted = (struct.data & STATUS_DELETED) != 0;
}

if (fDeleted)
writeln(jobId + " has completed");

Note that the job id is initialized before the ''deleted'' flag gets any
chance to set true. So it is not possible to use not initialized jobId.
Nevertheless, compler forces me to do the useless initialization. Do the
designers of ''clever'' robots forsee any overcoming for the limitations
they impose?



fDeleted = false;

uint jobId;
foreach (Struct struct in structures) {
if (struct.type == JOB) {
jobId = struct.id;
if (struct.dataType == STATUS)
fDeteted = (struct.data & STATUS_DELETED) != 0;
}
if (fDeleted)
writeln(jobId + " has completed");
Note that the job id is initialized before the ''deleted'' flag gets any
chance to set true. So it is not possible to use not initialized
jobId. Nevertheless, compler forces me to do the useless
initialization. Do the designers of ''clever'' robots forsee any
overcoming for the limitations they impose?

What if "structures" is empty and the code within the foreach never runs?

Hans Kesting


"valentin tihomirov" <no**************@yandex.ruschrieb im Newsbeitrag
news:OP*************@TK2MSFTNGP02.phx.gbl...

fDeleted = false;
uint jobId;

foreach (Struct struct in structures) {
if (struct.type == JOB) {
jobId = struct.id;
if (struct.dataType == STATUS)
fDeteted = (struct.data & STATUS_DELETED) != 0;
}

if (fDeleted)
writeln(jobId + " has completed");

Note that the job id is initialized before the ''deleted'' flag gets any
chance to set true. So it is not possible to use not initialized jobId.
Nevertheless, compler forces me to do the useless initialization. Do the
designers of ''clever'' robots forsee any overcoming for the limitations
they impose?

Hi Valentin,

I''m not sure, what exactly is your concern here.
Surely, this is one of the seldom cases, where a variable can''t be used
unassigned, but the compiler supposes it can. This doesn''t seem to be a
secial problem of C#, but for any language, that prohibits the use of
unassigned variables. (Though maybe C# is the only one until now, I''m not
sure).

Why don''t you initialize it to zero? performence considerations?

If you want a language, that allows the use of unassigned variables, you
will have to use another language.

Christof

PS You could try to use

unsafe {uint* p = &jobId;}

just before the loop. Probably the compiler will optimize this assignment
away.


这篇关于编译器的自信心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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