|
|
Hi,
the program in the end of this messages shows some strange behavior if
compiled on my system (Gentoo, GCC 3.3.5-20050130). The code has one call
to function read(), but it is executed twice, the first time before
execution of main (there are no warnings when compiling). I guess some
other call is wrongly linked to my read() function --- if I rename my
function or make it static, it works just fine.
Is this a known issue or do I have a very "special" system?
I experienced this strange behavior only on my system (Gentoo, GCC
3.3.5-20050130), an AMD64 with debian in my office compiles correctly, so
do the machines of some friendly guys on comp.lang.c.
Please compile the program in the end of the message and tell me if you can
reproduce the described behavior.
Any ideas on the cause ?
(I know read() is a system call, but linking system calls to a user defined
function without any warning seems to be a bad idea ;-) )
Command line used for compilation:
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 calc_parity.c
-systems with buggy behavior:
Gentoo, GCC 3.3.5-20050130
-program ok
gcc (GCC) 3.3.5-13 on debian (AMD64)
gcc (GCC) 3.2.3 20030502 (RedHat Linux 3.2.3-34)
gcc (GCC) 3.2.2 20030222 (Red Hat Linux 3.2.2-5)
gcc (GCC) 4.0.2 20051125 (Red Hat 4.0.2-8)
GCC 2.8.1 on Solaris 8 on Sparc
Sun compiler on Solaris 8 on Sparc
(please don't mind the program itself. It's a students work, presumably the
one with the most interesting execution on my computer)
------------------SNIP-------------------
#include <stdio.h>
int bit_calc(signed char);
int read(void);
int read(){
int value;
printf("please enter a number between -128 and 127: ");
scanf("%d",&value);
return(value);
}
int main(void){
signed char bits = 0;
int countbits=0;
printf("start of main()\n");
bits=read();
countbits = (bit_calc(bits));
printf("the paritybit is: %d\n",countbits);
return 0;
}
int bit_calc(signed char bits)
{
int count = 0;
int countbits = 0;
int mask = 0;
for(count=0;count<8;count++)
{
mask=(bits & (1 << count));
if (mask != 0)
{
countbits++;
}
}
return( countbits & 1);
}
------------------------------------------------------------------
Thanks,
Andreas
|
|