Re: output difference between VC and gcc

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux