GCC-Bug? (意外的“重载”) [英] GCC-Bug ? (Unintended "overloading")

查看:91
本文介绍了GCC-Bug? (意外的“重载”)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




代码片段在使用GCC编译时会导致奇怪的行为

(Linux,Gentoo 3.3.5.20050130-r1)。 read()被调用两次,虽然只调用一次,这是第一次调用main之前的第一次!

我想有一些调用另一个read-function我的函数错误地连接到
,如果read()被重命名或静态,它可以正常工作。

编译不会产生任何警告。任何人都可以向我解释这个行为

或者用其他编译器/操作系统重现它吗?


谢谢,

Andreas


---------------- SNIP ---------------

#include< stdio.h>

int bit_calc(签名字符位)

{/ *做点什么* /

}


int read(){

int value;

printf("请输入介于-128和127之间的数字:") ;

scanf("%d"& value);

return(value);

}


int main(){

printf(" start of main()\ n");

signed char bits = 0;


int countbits = 0;

bits = read();

// ...

返回0;

}

------------------------------ -----------

解决方案

Andreas Griesmayer说:





跟随一块鳕鱼当使用GCC编译时,e会导致奇怪的行为

(Linux,Gentoo 3.3.5.20050130-r1)。 read()被调用两次虽然只有

调用一次,这是第一次调用main之前的第一次!



read()是Linux系统上的系统调用。但是,如果你在

符合模式下调用编译器,则不应该发生冲突。


您的代码在我的系统上生成以下编译器诊断。 />

foo.c:3:警告:没有以前的原型`bit_calc''

foo.c:在函数`bit_calc'':

foo.c:2:警告:未使用的参数`bits''

foo.c:4:警告:控制到达非空函数的结尾

foo .c:顶级:

foo.c:6:警告:函数声明不是原型

foo.c:13:警告:函数声明不是' 'ta prototype

foo.c:在函数`main'':

foo.c:15:在'signed'之前解析错误''

foo.c:18:`bits''未声明(在此函数中首次使用)

foo.c:18 :(每个未声明的标识符仅报告一次

foo .c:18:对于它出现的每个功能。)

make:*** [foo.o]错误1


我修正了错误移动printf做有点,在主要部分。


在符合模式下编译时,代码在我的系统上正常工作,并且在不符合模式下编译时确实是


我猜有一些调用另一个读函数我的函数是错误的

链接到,



这几乎可以肯定是在/你的/系统上发生的事情。它不会发生在我的b $ b上,至少不会发生这两个编辑中的任何一个

行:


gcc - o foo foo.c





gcc -W -Wall -ansi -pedantic -Wformat-nonliteral -Wcast-align
-Wpointer-arith -Wbad-function-cast -Wmissing-prototypes

-Wstrict-prototypes -Wmissing-declarations -Winline -Wundef

-Wnested- externs -Wcast-qual -Wshadow -Wconversion -Wwrite-strings

-Wno-conversion -ffloat-store -O2 -o foo foo.c


- -

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上面的域名(但显然放弃了www)


Andreas Griesmayer写道:
< blockquote class =post_quotes>
read()被调用两次,虽然只有

调用一次,这是第一次调用main之前的第一次!



ok,那个句子有点扭曲......让我再试一次:函数read()是

执行两次虽然只调用一次来源......


---------------- SNIP -------------- -

#include< stdio.h>

int bit_calc(签名字符位)

{/ *做点什么* /

}


int read(){

int value;

printf("请输入一个数字在-128和127之间:);

scanf("%d"& value);

return(value);

}


int main(){

printf(" start of main()\ n");

签名char bits = 0;


int countbits = 0;

bits = read();

// .. 。

返回0;

}

--------------------- --------------------


-

Andreas Griesmayer


Andreas Griesmayer< ag ****** @ ist.tugraz.atwrites:




以下代码片段在使用GCC编译时会导致奇怪的行为

(Linux,Gentoo 3.3) .5.20050130-R1)。 read()被调用两次,虽然只调用一次,这是第一次调用main之前的第一次!

我想有一些调用另一个read-function我的函数错误地连接到
,如果read()被重命名或静态,它可以正常工作。

编译不会产生任何警告。任何人都可以向我解释这个行为

或者用其他编译器/操作系统重现它吗?



你是如何编制该系统的?

你试过gcc -std = c99 -Wall ....?


可能是gcc的正常模式有默认的
定义的POSIX东西。


问候

Friedrich


-

请删除just-for-news-通过电子邮件回复。


Hi,

following piece of code causes a strange behavior when compiled with GCC
(Linux, Gentoo 3.3.5.20050130-r1). read() is called twice although only
called once, the first time before the first statement of main !
I guess there is some call to another read-function my function is wrongly
linkted to, it works fine if read() is renamed or static.
Compilation does not produce any warnings. Can anyone explain this behavior
to me or reproduce it with another compiler/operating system?

thanks,
Andreas

---------------- SNIP ---------------
#include <stdio.h>
int bit_calc(signed char bits)
{ /* do something */
}

int read(){
int value;
printf("please enter a number between -128 and 127: ");
scanf("%d",&value);
return(value);
}

int main(){
printf("start of main()\n");
signed char bits = 0;

int countbits=0;
bits=read();
//...
return 0;
}
-----------------------------------------

解决方案

Andreas Griesmayer said:

Hi,

following piece of code causes a strange behavior when compiled with GCC
(Linux, Gentoo 3.3.5.20050130-r1). read() is called twice although only
called once, the first time before the first statement of main !

read() is a system call on Linux systems. If you invoke the compiler in
conforming mode, however, there should be no clash.

Your code generates the following compiler diagnostics on my system.

foo.c:3: warning: no previous prototype for `bit_calc''
foo.c: In function `bit_calc'':
foo.c:2: warning: unused parameter `bits''
foo.c:4: warning: control reaches end of non-void function
foo.c: At top level:
foo.c:6: warning: function declaration isn''t a prototype
foo.c:13: warning: function declaration isn''t a prototype
foo.c: In function `main'':
foo.c:15: parse error before `signed''
foo.c:18: `bits'' undeclared (first use in this function)
foo.c:18: (Each undeclared identifier is reported only once
foo.c:18: for each function it appears in.)
make: *** [foo.o] Error 1

I fixed the error by moving printf down a bit, in main.

The code works properly on my system when compiled in conforming mode, and
indeed when compiled in non-conforming mode!

I guess there is some call to another read-function my function is wrongly
linkted to,

That is almost certainly what''s happening here - on /your/ system. It
doesn''t happen on mine, at least not with either of these two compilation
lines:

gcc -o foo foo.c

and

gcc -W -Wall -ansi -pedantic -Wformat-nonliteral -Wcast-align
-Wpointer-arith -Wbad-function-cast -Wmissing-prototypes
-Wstrict-prototypes -Wmissing-declarations -Winline -Wundef
-Wnested-externs -Wcast-qual -Wshadow -Wconversion -Wwrite-strings
-Wno-conversion -ffloat-store -O2 -o foo foo.c

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)


Andreas Griesmayer wrote:

read() is called twice although only
called once, the first time before the first statement of main !

ok, that sentence is a bit twisted... let me try again: function read() is
executed twice although only called once by the source ...

---------------- SNIP ---------------
#include <stdio.h>
int bit_calc(signed char bits)
{ /* do something */
}

int read(){
int value;
printf("please enter a number between -128 and 127: ");
scanf("%d",&value);
return(value);
}

int main(){
printf("start of main()\n");
signed char bits = 0;

int countbits=0;
bits=read();
//...
return 0;
}
-----------------------------------------

--
Andreas Griesmayer


Andreas Griesmayer <ag******@ist.tugraz.atwrites:

Hi,

following piece of code causes a strange behavior when compiled with GCC
(Linux, Gentoo 3.3.5.20050130-r1). read() is called twice although only
called once, the first time before the first statement of main !
I guess there is some call to another read-function my function is wrongly
linkted to, it works fine if read() is renamed or static.
Compilation does not produce any warnings. Can anyone explain this behavior
to me or reproduce it with another compiler/operating system?

How did you have compiled that system?
Have you tried gcc -std=c99 -Wall .... ?

It could be that normal mode of gcc has the POSIX stuff defined by
default.

Regards
Friedrich

--
Please remove just-for-news- to reply via e-mail.


这篇关于GCC-Bug? (意外的“重载”)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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