Re: Function call evaluation order
- From: "Frederico Pissarra" <fredericopissarra@xxxxxxxxx>
- Date: Sat, 25 Mar 2006 17:50:46 -0300
"Tom Widmer [VC++ MVP]" <tom_usenet@xxxxxxxxxxx> wrote in message
news:u5s2PA3TGHA.4236@xxxxxxxxxxxxxxxxxxxxxxx
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
My intent isn't to make anyone angry at me here... hehe...
I'm testing every scenario you guys are posting here and trying to
understand why you consider those codes having "undefined" behaviour... What
I am seeing, as an experienced C/C++ developer, is that precedence and
associativity can explain everything here...
In the last piece of code I expected to see 0 0 0 and 3 3 3... and it is
what happen!
I can't see what is "undefined" here...
expressions using ++ or -- can be very confusing sometimes, but to me they
aren't "undefined"...
Until now all of you failed to define what "undefined" is... Igor Tandetnik
gave a nice view of "undefined" calling sequence (and that explanation I can
understand and accept)...
Please, don't be angry! :)
[]s
Fred
.
- Follow-Ups:
- Re: Function call evaluation order
- From: Tom Widmer [VC++ MVP]
- Re: Function call evaluation order
- From: Tim Roberts
- Re: Function call evaluation order
- From: Carl Daniel [VC++ MVP]
- 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
- Re: Function call evaluation order
- From: Tom Widmer [VC++ MVP]
- Function call evaluation order
- Prev by Date: can someone please help me with making this class
- Next by Date: Re: ERROR_DLL_INIT_FAILED - HELP please
- Previous by thread: Re: Function call evaluation order
- Next by thread: Re: Function call evaluation order
- Index(es):
Relevant Pages
|