Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

array variable subscript generate wrong code (2008) ?
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?

Report this thread to moderator Post Follow-up to this message
Old Post
brof777@gmail.com
04-18-08 10:03 AM


Re: array variable subscript generate wrong code (2008) ?
brof777@gmail.com wrote:
> 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?

This code has undefined behaviour, Dw++ is executed three times but it
is not defined when exactly that happens. This is quite classic example
like x = i++ + i++;

br
ismo

Report this thread to moderator Post Follow-up to this message
Old Post
Ismo Salonen
04-18-08 10:03 AM


Re: array variable subscript generate wrong code (2008) ?
On 18 avr, 07:01, brof...@gmail.com wrote:
> here is the code:
> DWORD SomeFunction(DWORD Dw_0, DWORD Dw_1, DWORD Dw_2)
> {
> =A0 =A0 =A0 =A0 return Dw_0 + Dw_1 + Dw_2;}
>
> void main()
> {
> =A0 =A0 =A0 =A0 DWORD Dw =3D 0;
> =A0 =A0 =A0 =A0 DWORD pDw[3] =3D {1,2,3};
> =A0 =A0 =A0 =A0 DWORD Dw_Sum =3D SomeFunction(pDw[Dw++], pDw[Dw++], pDw[Dw=[/color
]
++]);}
>
> 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?

Because the behaviour of this code is undefined, according to the C++
standard : The post-increment of the "++"  operator is a side-effect
of this operator (whose "main" effect is to return the value of Dw).
This side-effect should be applied at the next "sequence point" (in
standardese speaking) after the expression. Now, in your case, the
next sequence point is the call to SomeFunction, *after* evaluation of
all it's arguments.

What it means is that :
- the compiler is free to evaluate the arguments of SomeFunction in
any order (right to left, left to right, somehing else...).
- if the evaluation of those arguments have side-effect (which is your
case), those side-effect must be applied before the function is
called. However, exactly WHEN is undefined. The compiler is free to
apply them after evaluating all arguments (whis is obviously the
case), of after each argument evaluation, or anything else.

Conclusion : Never use several operations with side-effect in the same
expression : this is :
1) hardly readeable
2) undefined as far as the standard goes.

Arnaud



Report this thread to moderator Post Follow-up to this message
Old Post
adebaene@club-internet.fr
04-18-08 10:03 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

VC++ archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 12:47 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.