CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 21 of 21
  1. #16
    Join Date
    Sep 2010
    Posts
    15

    Re: Do functions compute before being called?

    One more question So you guys are saying that the order of evaluation in the statements I have written is undefined. E.g. D_Drmmr said,

    "No, the order in which output appears on the screen is the same as the order in which std::cout is called. The problem with your original program is that there are two calls to std::cout (one in sd and one in main), who's order is unspecified."


    But yet - the output of this program is ALWAYS A:

    "Input array element 0: 5
    Input array element 1: 7
    Input array element 2: 9
    7
    The standard deviation of these numbers is 1.63299."


    And never, for example, B:

    "Input array element 0: 5
    Input array element 1: 7
    Input array element 2: 9
    The standard deviation of these numbers is 7
    1.63299."


    If it were undefined surely the 7 would NOT ALWAYS be before the final sentence? What am I missing here? It seems to me that the order of evaluation is somehow defined.

    I am still unsure of when/where exactly my function is executed.
    Thanks again

  2. #17
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Do functions compute before being called?

    Quote Originally Posted by KingGhidorah View Post
    One more question So you guys are saying that the order of evaluation in the statements I have written is undefined. E.g. D_Drmmr said,
    It's not undefined, it's unspecified. That basically means the compiler is free to choose any order. Of course, the compiler won't choose something at random, so it will always produce the same evaluation order (at least, with the same configuration).
    Quote Originally Posted by KingGhidorah View Post
    If it were undefined surely the 7 would NOT ALWAYS be before the final sentence? What am I missing here? It seems to me that the order of evaluation is somehow defined.
    How do you know? Even "undefined" doesn't mean random.
    Quote Originally Posted by KingGhidorah View Post
    I am still unsure of when/where exactly my function is executed.
    Exactly.
    That's why you should change your code such that you can be sure.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  3. #18
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Do functions compute before being called?

    Quote Originally Posted by KingGhidorah View Post
    If it were undefined surely the 7 would NOT ALWAYS be before the final sentence? What am I missing here? It seems to me that the order of evaluation is somehow defined.
    The issue is not what your current compiler will do, the issue is what any compiler may do with your code. If you took your code, and compiled it on a different compiler, you cannot guarantee the order will be the same.
    I am still unsure of when/where exactly my function is executed.
    That's the point -- you are not the only one unsure -- even the creator of the C++ language, Bjarne Stroustrup, is unsure.

    The order of processing could be all jumbled up for some reason (the first parameter is processed first, then the fifth parameter is processed second, then the second parameter is processed third, etc.). The only thing guaranteed is at the other end when that function you're calling starts, it will have the correct values.

    Regards,

    Paul McKenzie

  4. #19
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: Do functions compute before being called?

    A really simple demonstration would be this.

    Code:
    int x = 0;
    
    F(++x, ++x, ++x);
    Parameter parsing is usually either left to right or right to left.
    So F is likely to be called with parameters 1, 2, 3 or 3, 2, 1
    It's possible it could also be called with any other permutation.
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  5. #20
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Do functions compute before being called?

    Quote Originally Posted by JohnW@Wessex View Post
    A really simple demonstration would be this.

    Code:
    int x = 0;
    
    F(++x, ++x, ++x);
    Parameter parsing is usually either left to right or right to left.
    So F is likely to be called with parameters 1, 2, 3 or 3, 2, 1
    It's possible it could also be called with any other permutation.
    Actually, its even worst than that, because argument evaluation and argument passing do not have to be done at the same time. In this example, the return type of ++x is a reference. So even if the result of the first ++x is 1, the evalutation of the second parameter changes the result of the first argument, after its evaluation, but before its passed, so you could even end up calling f(3, 3, 3);

    This, on my system:
    Code:
    #include <iostream>
    
    void f(int i, int j, int k)
    {
      std::cout << i << j << k << std::endl;
    }
    
    int main()
    {
      int x1 = 0;
      int x2 = 0;
      f(++x1, ++x1, ++x1);
      f(x2++, x2++, x2++);
    }
    prints

    Code:
    333
    210
    Last edited by monarch_dodra; March 8th, 2011 at 04:55 AM.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  6. #21
    Join Date
    Oct 2008
    Posts
    1,456

    Re: Do functions compute before being called?

    Quote Originally Posted by JohnW@Wessex View Post
    It's possible it could also be called with any other permutation.
    Quote Originally Posted by monarch_dodra View Post
    Actually, its even worst than that, because argument evaluation and argument passing do not have to be done at the same time.

    Actually, its even worst than that , because the evaluation of such a code gives UB ( in that you can rearrange the evaluation sequence in such a way to read and modify the same scalar variable twice between two sequence points )

    for example the code

    Code:
    #include <iostream>
    
    void f(int i, int j, int k)
    {
      std::cout << i << j << k << std::endl;
    }
    
    int main()
    {
      int x1 = 0;
      int x2 = 0;
    
      f(++x1 + x2++, ++x1 + x2++, ++x1 + x2++);
    }
    outputs "321" on my system; as you can see, there's no rearrangement of the "basic" operations that reproduces the output, therefore UB has been invoked in this case.

Page 2 of 2 FirstFirst 12

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured