Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / April 2008

Tip: Looking for answers? Try searching our database.

array variable subscript generate wrong code (2008) ?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
brof777@gmail.com - 18 Apr 2008 06:01 GMT
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?
Ismo Salonen - 18 Apr 2008 08:13 GMT
> here is the code:
> DWORD SomeFunction(DWORD Dw_0, DWORD Dw_1, DWORD Dw_2)
[quoted text clipped - 13 lines]
> 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
adebaene@club-internet.fr - 18 Apr 2008 09:23 GMT
On 18 avr, 07:01, brof...@gmail.com wrote:
> here is the code:
> DWORD SomeFunction(DWORD Dw_0, DWORD Dw_1, DWORD Dw_2)
[quoted text clipped - 13 lines]
> 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
David Wilkinson - 18 Apr 2008 16:29 GMT
> here is the code:
> DWORD SomeFunction(DWORD Dw_0, DWORD Dw_1, DWORD Dw_2)
[quoted text clipped - 13 lines]
> push pDw[0] 3 times, then increase Dw by 3 times.
> Can anyone tell me why?

brof:

Please don't multi-post. This question has nothing to do with .NET, so it should
not be here anyway.

Signature

David Wilkinson
Visual C++ MVP


Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.