这为什么有效? [英] why does this work ?

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

问题描述

为什么f会被退回? (用gcc编译)。


#include< stdio.h>


int func(int a);


main()

{int f;

f = func(7);

printf(" f =% d",f);


}

int func(int a)

{

int f,d = 100;

if(a< 10){

f = d;

}

}


Ta,

Bamber。

Why does f get returned ? (compiled with gcc).

#include<stdio.h>

int func(int a);

main()
{ int f;
f=func(7);
printf("f=%d",f);

}
int func(int a)
{
int f,d = 100;
if (a<10){
f = d;
}
}

Ta,

Bamber.

推荐答案

Bamber< ba ****************** @ yahoo.co.uk>潦草地写道:
Bamber <ba******************@yahoo.co.uk> scribbled the following:
为什么f返回? (用gcc编译)。


没有。你错误地依赖于实现依赖的行为

正常运行。

#include< stdio.h>
int func(int a);
main()
{int f;
f = func(7);
printf(" f =%d",f);
}
int func(int a)
{
int f,d = 100;
if(a< 10){
f = d;
}
}
Why does f get returned ? (compiled with gcc).
It doesn''t. You''re mistaking implementation-dependent behaviour for
normal operation.
#include<stdio.h> int func(int a); main()
{ int f;
f=func(7);
printf("f=%d",f);

}
int func(int a)
{
int f,d = 100;
if (a<10){
f = d;
}
}




当控制到达func()的末尾时,返回的值为

不确定。当赋值f = func(7)在main()中完成时,C

实现尝试从没有任何值的地方获取值。在

的一些实现中,这是在赋值发生之前放在堆栈上的最后一个值,但是没有要求实现

应该做这个。没有要求实现*首先* *

堆栈。

一般来说,你只是(非)幸运。

-

/ - Joona Palaste(pa*****@cc.helsinki.fi)---------------- ----------- \

| 飞翔的柠檬树中的金鸡王G ++ FR FW + M-#108 D + ADA N +++ |

| http://www.helsinki.fi/~palaste W ++ B OP + |

\ -----------------------------------------芬兰的规则! ------------ /

冰淇淋销售不知何故导致溺水:两者都发生在夏季。

- Antti Voipio& Arto Wikla



When control reaches the end of func(), the returned value is
indeterminate. When the assignment f=func(7) is done in main(), the C
implementation tries to get a value from where there isn''t any. On
some implementations, this is the last value put on a stack before
the assignment happens, but there is no requirement that implementations
should do this. There is no requirement for implementations to *have* a
stack in the first place.
Generally, you just got (un)lucky.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"Ice cream sales somehow cause drownings: both happen in summer."
- Antti Voipio & Arto Wikla


问候。


文章< 37 ************** ************@posting.google.com>,Bamber写道:
Greetings.

In article <37**************************@posting.google.com >, Bamber wrote:
为什么f会被退回?




它没有。事实上,没有任何东西可以归还,因为在您的程序中的任何地方都没有返回

语句。


问候,

Tristan


-

_

_V.-o Tristan Miller [en,(fr,de,ia)] ><空间有限

/ |` - '' - = - = - = - = - = - = - = - = - = - = - = - = - = - = - =<> ;在ha句中,所以很难

(7_ \\ http://www.nothingisreal.com/ ><完成你的内容



It doesn''t. In fact, nothing gets returned, since there is no return
statement to be found anywhere in your program.

Regards,
Tristan

--
_
_V.-o Tristan Miller [en,(fr,de,ia)] >< Space is limited
/ |`-'' -=-=-=-=-=-=-=-=-=-=-=-=-=-=-= <> In a haiku, so it''s hard
(7_\\ http://www.nothingisreal.com/ >< To finish what you


In< 37 ******** ******************@posting.google.com> ba ****************** @ yahoo.co.uk (Bamber)写道:
In <37**************************@posting.google.com > ba******************@yahoo.co.uk (Bamber) writes:
为什么f返回?(用gcc编译)。
#include< stdio.h>

int func(int a);

main()
{int f;
f = func(7);
printf(" f =%d",f);


此格式字符串中缺少换行符。

} int func(int a)
{
int f,d = 100;
if(a< 10){
f = d;
}
}
Why does f get returned ? (compiled with gcc).

#include<stdio.h>

int func(int a);

main()
{ int f;
f=func(7);
printf("f=%d",f);
There is a newline character missing in this format string.

}
int func(int a)
{
int f,d = 100;
if (a<10){
f = d;
}
}




纯粹你的代码通过使用一个实际上没有返回的函数返回的

值来调用未定义的行为。 nything,所以

*任何事情*都可能发生。


使用相同的编译器编译,在不同的平台上,输出是

不同:


导师:〜/ tmp 11> uname -a

SunOS导师5.8 Generic_108528-11 sun4u sparc

导师:〜/ tmp 12> cat test.c

#include< stdio.h>


int func(int a);


main()

{int f;

f = func(7);

printf(" f =%d \ n", f);


}

int func(int a)

{

int f, d = 100;

if(a< 10){

f = d;

}

}

导师:〜/ tmp 13> gcc test.c

导师:〜/ tmp 14> ./a.out

f = 7

这在func()返回之前看起来不像f'的值,是吗?

根据你的逻辑,我应该开始问自己:为什么func()会返回其参数的

值? ;-)


道德:试图理解为什么一块损坏的代码行为与它的行为方式完全是浪费时间。 />

Dan

-

Dan Pop

DESY Zeuthen,RZ集团

电子邮件: Da*****@ifh.de


这篇关于这为什么有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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