奇数代码的奇怪行为 [英] Odd behavior with odd code

查看:76
本文介绍了奇数代码的奇怪行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include< stdio.h>

#include< stdlib.h>


int main(int argc,char ** argv)

{

looper:

printf("%d \ nn",argc);

printf ("%x \ n",&&& looper);

if(argc 0)

((int(*)(int,char **) ))&& looper)(0,argv);

返回0;

}


Linux版本2.6。 17-10-386(root @ vernadsky)(gcc版本4.1.2

20060928(预发布)(Ubuntu 4.1.1-13ubuntu5))#2 Fri Oct 13 18:41:40

UTC 2006(Ubuntu 2.6.17-10.33-386)


gcc版本4.1.2 20060928(预发布)(Ubuntu 4.1.1-13ubuntu5)


在这个方框中,上面的代码编译但是在无限循环中运行。

而不是按下堆栈指针并调用标签位置

a函数与我所期望的给定的参数,编译器

反而表现为好像它是一个简单的goto并重用原点al $ / $
参数。


gdb backtrace只能看到一帧。

#include <stdio.h>
#include <stdlib.h>

int main( int argc , char ** argv )
{
looper:
printf( "%d\n" , argc ) ;
printf( "%x\n" , &&looper ) ;
if( argc 0 )
((int(*)(int,char**))&&looper)( 0 , argv ) ;
return 0 ;
}

Linux version 2.6.17-10-386 (root@vernadsky) (gcc version 4.1.2
20060928 (prerelease) (Ubuntu 4.1.1-13ubuntu5)) #2 Fri Oct 13 18:41:40
UTC 2006 (Ubuntu 2.6.17-10.33-386)

gcc version 4.1.2 20060928 (prerelease) (Ubuntu 4.1.1-13ubuntu5)

On this box the code above compiles but runs in an infinite loop.
Instead of pushing the stack pointer and calling the label location as
a function with the arguments given as I expected, the compiler
instead acts as though it was a simple goto and reuses the original
arguments.

gdb backtrace sees only a single frame.

推荐答案

Michael Speer写道:
Michael Speer wrote:

#include< stdio.h>

#include< stdlib.h>


int main(int argc,char ** argv)

{

looper:

printf("%d \ n" ,argc);

printf("%x \ n",&& looper);

if(argc 0)

((int(*)(int,char **))&& looper)(0,argv);

返回0;

}


Linux版本2.6.17-10-386(root @ vernadsky)(gcc版本4.1.2

20060928(预发布)(Ubuntu 4.1.1-13ubuntu5)) #2 Fri Oct 13 18:41:40

UTC 2006(Ubuntu 2.6.17-10.33-386)


gcc版本4.1.2 20060928(预发布) )(Ubuntu 4.1.1-13ubuntu5)


在这个框中,上面的代码编译但是在无限循环中运行。

而不是推送堆栈指针和将标签位置称为

a函数,并按照我的预期给出参数,编译器

反而表现为好像是一个简单的goto并重用原始的

参数。


gdb backtrace只能看到一个帧。
#include <stdio.h>
#include <stdlib.h>

int main( int argc , char ** argv )
{
looper:
printf( "%d\n" , argc ) ;
printf( "%x\n" , &&looper ) ;
if( argc 0 )
((int(*)(int,char**))&&looper)( 0 , argv ) ;
return 0 ;
}

Linux version 2.6.17-10-386 (root@vernadsky) (gcc version 4.1.2
20060928 (prerelease) (Ubuntu 4.1.1-13ubuntu5)) #2 Fri Oct 13 18:41:40
UTC 2006 (Ubuntu 2.6.17-10.33-386)

gcc version 4.1.2 20060928 (prerelease) (Ubuntu 4.1.1-13ubuntu5)

On this box the code above compiles but runs in an infinite loop.
Instead of pushing the stack pointer and calling the label location as
a function with the arguments given as I expected, the compiler
instead acts as though it was a simple goto and reuses the original
arguments.

gdb backtrace sees only a single frame.



这个小组讨论ISO C.你的代码使用gcc特定扩展和

因此它的行为超出了这个组的范围。也许gcc

邮件列表或组更合适。

This group discusses ISO C. Your code uses gcc specific extensions and
thus it''s behaviour is outside the scope of this group. Maybe a gcc
mailing list or group would be more appropriate.


文章< 11 ****** ***************@v45g2000cwv.googlegroups。 com>,

Michael Speer< kn ******* @ gmail.comwrote:
In article <11*********************@v45g2000cwv.googlegroups. com>,
Michael Speer <kn*******@gmail.comwrote:

> #include< stdio.h>
#include< stdlib.h>
>#include <stdio.h>
#include <stdlib.h>



您似乎没有使用stdlib.h中的任何内容

You do not appear to be using anything from stdlib.h


> int main(int argc,char ** argv)
$
looper:

printf("%d \ nn",argc);

printf("%x \ n",&& looper);
>int main( int argc , char ** argv )
{
looper:
printf( "%d\n" , argc ) ;
printf( "%x\n" , &&looper ) ;



标签不是对象,也不能使用其地址。

标签甚至与对象不在同一名称空间中。


如果您确实设法获取了标签的地址,那么

a%x格式将不是正确的打印格式

输出地址。你需要%p来打印指针。

A label is not an object, and cannot have its address taken.
A label is not even in the same namespace as objects.

If you did manage to take the address of a label, then
a %x format would not be the correct format with which to print
out the address. You need %p to print out pointers.


if(argc 0)

((int(*)(int,char **) ))&& looper)(0,argv);

返回0;
}
if( argc 0 )
((int(*)(int,char**))&&looper)( 0 , argv ) ;
return 0 ;
}


> Linux版本2.6.17-10-386(root @ vernadsky)(gcc版本4.1.2
20060928(预发布)(Ubuntu 4.1.1-13ubuntu5))#2 Fri Oct 13 18:41:40 < br> UTC 2006(Ubuntu 2.6.17-10.33-386)

gcc版本4.1.2 20060928(预发布)(Ubuntu 4.1.1-13ubuntu5)

关于此框上面的代码编译但在无限循环中运行。
>Linux version 2.6.17-10-386 (root@vernadsky) (gcc version 4.1.2
20060928 (prerelease) (Ubuntu 4.1.1-13ubuntu5)) #2 Fri Oct 13 18:41:40
UTC 2006 (Ubuntu 2.6.17-10.33-386)

gcc version 4.1.2 20060928 (prerelease) (Ubuntu 4.1.1-13ubuntu5)

On this box the code above compiles but runs in an infinite loop.



你没用C编译,用gcc编译。 gcc实现了

a类C语言,但是没有实现C,除非你使用一个

的编译选项来强制它编译C编译器的方式

应该。要获得gcc实施的类似C语言的帮助,你需要在gcc新闻组中提问。 (你不会是/ b $ b能够将你的程序编译成真正的C.)

You didn''t compile with C, you compiled with gcc. gcc implements
a C-like language, but does not implement C unless you use a
number of compile options to force it to compile the way a C compiler
should. For assistance with the C-like language implemented
by gcc, you would need to ask in a gcc newsgroup. (You won''t be
able to compile your program as real C.)


>而不是推动堆栈指针和将标签位置调用为具有我预期的给定参数的函数,编译器反而表现为好像是一个简单的goto并重用原始的
参数。
>Instead of pushing the stack pointer and calling the label location as
a function with the arguments given as I expected, the compiler
instead acts as though it was a simple goto and reuses the original
arguments.



当你违反C语义时会发生任何事情。


-

好​​的,流行语只要。两个音节,上衣。 - Laurie Anderson

Anything can happen when you violate C semantics.

--
Okay, buzzwords only. Two syllables, tops. -- Laurie Anderson


" Michael Speer" < kn ******* @ gmail.comwrote:
"Michael Speer" <kn*******@gmail.comwrote:

> #include< stdio.h>
#include< stdlib .h>

int main(int argc,char ** argv)
$

printf("% d \ n",argc);

printf("%x \ n",&& looper);

if(argc 0)

((int(*)(int,char **))&& looper)(0,argv);

返回0;
}
...

在这个方框中,上面的代码编译但是在一个无限循环中运行。
而不是推送堆栈指针并调用标签位置
一个函数按照我的预期给出的参数,编译器反而表现得好像它是一个简单的goto并重用原始的
参数。
>#include <stdio.h>
#include <stdlib.h>

int main( int argc , char ** argv )
{
looper:
printf( "%d\n" , argc ) ;
printf( "%x\n" , &&looper ) ;
if( argc 0 )
((int(*)(int,char**))&&looper)( 0 , argv ) ;
return 0 ;
}
...

On this box the code above compiles but runs in an infinite loop.
Instead of pushing the stack pointer and calling the label location as
a function with the arguments given as I expected, the compiler
instead acts as though it was a simple goto and reuses the original
arguments.



我不明白&& looper是什么意思,

的地址是一个地址标签吗?" ;.标签既不是功能也不是对象,

所以一元&无法应用它。该计划产生了

预期结果。在其他一些纯粹的运气系统中。 (或缺少



来自WG14 / N1124委员会草案? 2005年5月6日ISO / IEC 9899:TC2

6.5.3.2地址和间接运营商


限制


1一元&的操作数。运算符应该是一个函数

指定符,一个[]或一元*运算符的结果,或一个左值,

指定一个不是位字段的对象未使用

声明寄存器存储类说明符。


2一元*运算符的操作数应具有指针类型。


语义

3一元&运算符产生其操作数的地址。如果

操作数的类型为?? ?? ??,则结果具有类型?? ??的指针类型。如果

操作数是一元*运算符的结果,那么该运算符既不是b $ b也不是&运算符被评估,结果就好像两个都被省略了,除了对运算符的约束仍然适用,并且

结果不是左值。同样,如果操作数是[]运算符的结果

,那么&运算符和[]所暗示的一元*是
被评估,结果就好像&运算符

被删除,[]运算符被更改为+运算符。

否则,结果是指向对象或函数的指针

由其操作数指定。


Roberto Waltman


[请回复小组,

返回地址是无效]

I don''t understand what is the meaning of &&looper, "the address of
the address of a label?". A label is neither a function nor an object,
so the unary & can not be applied to it. It this program produced
"expected results" in some other system that is sheer luck. (Or lack
thereof)
From WG14/N1124 Committee Draft ? May 6, 2005 ISO/IEC 9899:TC2

6.5.3.2 Address and indirection operators

Constraints

1 The operand of the unary & operator shall be either a function
designator, the result of a [] or unary * operator, or an lvalue that
designates an object that is not a bit-field and is not declared with
the register storage-class specifier.

2 The operand of the unary * operator shall have pointer type.

Semantics

3 The unary & operator yields the address of its operand. If the
operand has type ??type??, the result has type ??pointer to type??. If
the operand is the result of a unary * operator, neither that operator
nor the & operator is evaluated and the result is as if both were
omitted, except that the constraints on the operators still apply and
the result is not an lvalue. Similarly, if the operand is the result
of a [] operator, neither the & operator nor the unary * that is
implied by the [] is evaluated and the result is as if the & operator
were removed and the [] operator were changed to a + operator.
Otherwise, the result is a pointer to the object or function
designated by its operand.

Roberto Waltman

[ Please reply to the group,
return address is invalid ]


这篇关于奇数代码的奇怪行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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