Re: Function call evaluation order
- From: "Tom Widmer [VC++ MVP]" <tom_usenet@xxxxxxxxxxx>
- Date: Fri, 24 Mar 2006 18:23:34 +0000
Frederico Pissarra wrote:
And think about the standard strcpy() function... It is implemented in C like this:
char *strcpy(char *dest, char *src)
{
char *sTemp = *dest;
while (*dest++ = *src++);
return dest;
}
if the post increments are supposed to be evaluated sequentially the routine will never work!
Of course it will! dest and src are different objects. We're talking about modifying the same object multiple times without intervening sequence points.
What do you think should be printed in the following two cases:
#include <iostream>
int main()
{
std::cout << "post\n";
int i = 0;
std::cout << i++ << ' ' << i++ << ' '<< i++ << '\n';
std::cout << "pre\n";
i = 0;
std::cout << ++i << ' ' << ++i << ' '<< ++i << '\n';
}
I get different results with and without optimization on VC7.1! The point is that precedence only imposes a *partial* ordering - beyond that ordering (that the first thing be output before the second), everything else can happen in any order, and because the same object is being modified, it is undefined (as opposed to unspecified) behaviour in any case. Again, read the C faq links I posted.
If you still don't understand after everything you've seen, never mind!
Tom
.
- Follow-Ups:
- Re: Function call evaluation order
- From: Frederico Pissarra
- Re: Function call evaluation order
- From: Frederico Pissarra
- Re: Function call evaluation order
- References:
- Function call evaluation order
- From: Cheng
- Re: Function call evaluation order
- From: Frederico Pissarra
- Re: Function call evaluation order
- From: Carl Daniel [VC++ MVP]
- Re: Function call evaluation order
- From: Frederico Pissarra
- Re: Function call evaluation order
- From: Carl Daniel [VC++ MVP]
- Re: Function call evaluation order
- From: Frederico Pissarra
- Re: Function call evaluation order
- From: Carl Daniel [VC++ MVP]
- Re: Function call evaluation order
- From: Carl Daniel [VC++ MVP]
- Re: Function call evaluation order
- From: Frederico Pissarra
- Function call evaluation order
- Prev by Date: Re: Function call evaluation order
- Next by Date: Re: Show DLL that is loading?
- Previous by thread: Re: Function call evaluation order
- Next by thread: Re: Function call evaluation order
- Index(es):
Relevant Pages
|