This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: output difference between VC and gcc
- From: Mihai DonÈu <mihai dot dontu at gmail dot com>
- To: gcc-help at gcc dot gnu dot org
- Cc: Peter <one2001boy at yahoo dot com>
- Date: Sat, 17 Jul 2010 07:17:22 +0300
- Subject: Re: output difference between VC and gcc
- References: <624538.15375.qm@web55801.mail.re3.yahoo.com>
On Saturday 17 July 2010 06:48:35 Peter wrote:
> Hi,
> I have the following code compiled in both microsoft VC 2008 and gcc
> version 4.1.2 20071124.
>
> The output is different.
>
> #include<stdio.h>
> void main(void)
> {
> int i,tab[]={1,2,3,4,5,6,7,8,9,10};
>
> i=0;
> while(i<10){
> printf("%d ",i);
> printf("%d:%d\n",i,tab[i++]);
> }
> }
>
> [...]
>
> based on C standard, whose output is correct?
> thanks.
GCC's, although it's interesting that visual studio does what one expects:
print i, print tab[i], then increment i. However, keeping in mind that
parameters are pushed on stack in reverse:
mov 0xfffffff4(%ebp),%eax
pushl 0xffffffb8(%ebp,%eax,4)
lea 0xfffffff4(%ebp),%eax
incl (%eax)
pushl 0xfffffff4(%ebp)
push $0x80484f1
call 80482b0 <_init+0x38>
things actually happen: print tab[i], print ( i + 1 ). Given the right
options, gcc can warn about this:
gcc -W -Wall test.c
...
test.c: In function `main':
test.c:9: warning: operation on `i' may be undefined
Unfortunately, many programmers don't know these details and VS helps avoid
undesired results:
mov ecx, [ebp+var_4]
mov edx, [ebp+ecx*4+var_2C]
mov [ebp+var_30], edx
mov eax, [ebp+var_30]
push eax
mov ecx, [ebp+var_4]
push ecx
push offset aDD
call _printf
add esp, 0Ch
mov edx, [ebp+var_4]
add edx, 1 ; <---- i++
mov [ebp+var_4], edx
Thumb's up to VS. :-)
--
Mihai DonÈu