here is the code:
DWORD SomeFunction(DWORD Dw_0, DWORD Dw_1, DWORD Dw_2)
{
return Dw_0 + Dw_1 + Dw_2;
}
void main()
{
DWORD Dw = 0;
DWORD pDw[3] = {1,2,3};
DWORD Dw_Sum = SomeFunction(pDw[Dw++], pDw[Dw++], pDw[Dw++]);
}
I expected VC++ 2008 generate code as SomeFunction(3,2,1) or
SomeFunction(1,2,3), however, the real code is like
SomeFunction(1,1,1)
I debug the and watch assembly code, find the code was generated with
push pDw[0] 3 times, then increase Dw by 3 times.
Can anyone tell me why?

Re: array variable subscript generate wrong code (2008) ? by Ulrich

Ulrich
Fri Apr 18 02:25:45 CDT 2008

brof777@gmail.com wrote:
> DWORD Dw = 0;
> DWORD pDw[3] = {1,2,3};
> DWORD Dw_Sum = SomeFunction(pDw[Dw++], pDw[Dw++], pDw[Dw++]);

You are modifying 'Dw' several times without a so-called 'sequence point' in
between. The behaviour of the above code is basically unspecified. Just
don't do that.

Uli

--
C++ FAQ: http://parashift.com/c++-faq-lite

Sator Laser GmbH
Geschäftsführer: Michael Wöhrmann, Amtsgericht Hamburg HR B62 932

Re: array variable subscript generate wrong code (2008) ? by adebaene

adebaene
Fri Apr 18 09:21:20 CDT 2008

On 18 avr, 09:25, Ulrich Eckhardt <eckha...@satorlaser.com> wrote:
> brof...@gmail.com wrote:
> > DWORD Dw = 0;
> > DWORD pDw[3] = {1,2,3};
> > DWORD Dw_Sum = SomeFunction(pDw[Dw++], pDw[Dw++], pDw[Dw++]);
>
> You are modifying 'Dw' several times without a so-called 'sequence point' in
> between. The behaviour of the above code is basically unspecified. Just
> don't do that.
>

Nonetheless, as a Quality of Implementation issue, it would be nice
that the compiler at least gave a warning fot this kind of
constructs... On the other hand, I suspect there is too much old 'C'
code relying on this kind of stuff to make the compiler more strict...

Arnaud

Re: array variable subscript generate wrong code (2008) ? by Barry

Barry
Sat Apr 19 02:00:17 CDT 2008

On Fri, 18 Apr 2008 07:21:20 -0700 (PDT), adebaene@club-internet.fr
wrote:

>On 18 avr, 09:25, Ulrich Eckhardt <eckha...@satorlaser.com> wrote:
>> brof...@gmail.com wrote:
>> > DWORD Dw = 0;
>> > DWORD pDw[3] = {1,2,3};
>> > DWORD Dw_Sum = SomeFunction(pDw[Dw++], pDw[Dw++], pDw[Dw++]);
>>
>> You are modifying 'Dw' several times without a so-called 'sequence point' in
>> between. The behaviour of the above code is basically unspecified. Just
>> don't do that.
>>
>
>Nonetheless, as a Quality of Implementation issue, it would be nice
>that the compiler at least gave a warning fot this kind of
>constructs... On the other hand, I suspect there is too much old 'C'
>code relying on this kind of stuff to make the compiler more strict...
>
This has always been undefined behavior in C. Any code that relies on
a particular behavior is seriously defective.


Remove del for email

Re: array variable subscript generate wrong code (2008) ? by adebaene

adebaene
Mon Apr 21 09:21:18 CDT 2008

On 19 avr, 09:00, Barry Schwarz <schwa...@dqel.com> wrote:
> On Fri, 18 Apr 2008 07:21:20 -0700 (PDT), adeba...@club-internet.fr
> wrote:
>
>
>
>
>
> >On 18 avr, 09:25, Ulrich Eckhardt <eckha...@satorlaser.com> wrote:
> >> brof...@gmail.com wrote:
> >> > DWORD Dw =3D 0;
> >> > DWORD pDw[3] =3D {1,2,3};
> >> > DWORD Dw_Sum =3D SomeFunction(pDw[Dw++], pDw[Dw++], pDw[Dw++]);
>
> >> You are modifying 'Dw' several times without a so-called 'sequence poin=
t' in
> >> between. The behaviour of the above code is basically unspecified. Just=

> >> don't do that.
>
> >Nonetheless, as a Quality of Implementation issue, it would be nice
> >that the compiler at least gave a warning fot this kind of
> >constructs... On the other hand, I suspect there is too much old 'C'
> >code relying on this kind of stuff to make the compiler more strict...
>
> This has always been undefined behavior in C. =A0Any code that relies on
> a particular behavior is seriously defective.

So, it is a good reason for the compiler to give a warning, don't you
think?

Arnaud