Re: Function call evaluation order



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
.



Relevant Pages

  • Re: is strncpy useful at all?
    ... > You return a pointer to the terminating null byte of 'dest'. ... char* tstrncpy; ... void runTest(FP_STRNCPY fp, char* dest, char* src, size_t maxlen, ... runTest(tstrncpy, dest, src, sizeof src - 1, DEST_SIZE); ...
    (alt.comp.lang.learn.c-cpp)
  • Automatic way to test performance optimizations
    ... But I would also like to test, that the optimization is used in the ... Does Sun remove all log statements from its JDK src before ... Objectdest, ... int high, ...
    (comp.lang.java.programmer)
  • Re: Wrong output - eliminate spaces in sentence.
    ... char *source, *dest; ... int main(int argc, char* *argv) ... that is the last valid location in argv, ...
    (comp.lang.c)
  • Re: Function call evaluation order
    ... char *strcpy(char *dest, char *src) ... dest and src are different objects. ... gave a nice view of "undefined" calling sequence (and that explanation I can ...
    (microsoft.public.vc.language)
  • Re: Function call evaluation order
    ... char *strcpy(char *dest, char *src) ... if the post increments are supposed to be evaluated sequentially the ... dest and src are different objects. ...
    (microsoft.public.vc.language)