CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1
    Join Date
    Jun 2002
    Posts
    137

    A question for C++ expert on ++a and a++

    This is the question for C++ expert, what will be the output of following codes and why?

    Thanks a lot in advance!

    Code:
    #include <iostream>
    
    int main()
    { 
        int a = 5;
        int d = a++;
        a = 5;
        int b = a++ * ++a;
        a = 5;
        int c = ++a * a++;
        a = 5;
        int e = (a++) * (1);
        std::cout << "b = " << b << std::endl;
        std::cout << "c = " << c << std::endl;
        std::cout << "d = " << d << std::endl;
        std::cout << "e = " << e << std::endl;
    }

  2. #2
    Join Date
    Oct 2002
    Location
    Austria
    Posts
    1,284

    Re: A question for C++ expert on ++a and a++

    I am not an expert ( but who is ? ).
    The question about "what" can easily be answered by just running the program.
    The question about "why" cannot fully be answered because of undefined behaviour.
    Kurt

  3. #3
    Join Date
    Jun 2002
    Posts
    137

    Re: A question for C++ expert on ++a and a++

    Yes, but I prefer you to think about the answer before run it in your platform.

    I cannot answer why the output it is, so I think some expert can explain it. And thanks to explain more about "undefined behaviour", you mean the result will be different based on different platform and compiler?

  4. #4
    Join Date
    Nov 2006
    Location
    Barcelona - Catalonia
    Posts
    364

    Re: A question for C++ expert on ++a and a++

    Why 'undefined behaviour'? I think it's clear, isn't it?
    Post-increment is performed after the expression is evaluated, and pre-increment before.
    For example:
    Code:
    int b = a++ * ++a;
    First evaluated is ++a, so it becomes 6. Then it's performed b = a * a = 36, then a++, so becomes 7. Where is the 'undefined behaviour'?

    Albert.
    Please, correct me. I'm just learning.... and sorry for my english :-)

  5. #5
    Join Date
    Dec 2004
    Location
    Poland
    Posts
    1,165

    Re: A question for C++ expert on ++a and a++

    Like ZuK said, results are undefined in C++: http://parashift.com/c++-faq-lite/mi...html#faq-39.15
    B+!
    'There is no cat' - A. Einstein

    Use &#91;code] [/code] tags!

    Did YOU share your photo with us at CG Members photo gallery ?

  6. #6
    Join Date
    Oct 2002
    Location
    Austria
    Posts
    1,284

    Re: A question for C++ expert on ++a and a++

    As far as I know * is not a sequence point it is possible that either ++a or a++ is evaluated first.
    Kurt

  7. #7
    Join Date
    Jun 2002
    Posts
    137

    Re: A question for C++ expert on ++a and a++

    yes, I guess it is the reason. Thanks a lot guys!

    Code:
    int operator *(const int & lhs, const int & rhs);
    in the function operator , no compiler can guarantee that lhs will be processed before rhs, maybe that is why it is called"undefined behaviour".

    Kurt

  8. #8
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: A question for C++ expert on ++a and a++

    Correct.

    btw: This is one of the reasons to NEVER overload operators "&&" and "||". By specification the native versions "short-curcuit", and any overload will evaluate BOTH!!!
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  9. #9
    Join Date
    Nov 2006
    Location
    Barcelona - Catalonia
    Posts
    364

    Re: A question for C++ expert on ++a and a++

    Quote Originally Posted by Hobson
    Like ZuK said, results are undefined in C++: http://parashift.com/c++-faq-lite/mi...html#faq-39.15
    Ok, I thought it worked in different way. Thanks for explanation.

    Albert.
    Please, correct me. I'm just learning.... and sorry for my english :-)

  10. #10
    Join Date
    May 2007
    Posts
    4

    Re: A question for C++ expert on ++a and a++

    First of all is good to return 0 because you use int main() and a value must be returned.++a increase the a by 1 and then use the new value of a in the expression.a++ use the current value in the expression and then increase the a by 1

  11. #11
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: A question for C++ expert on ++a and a++

    Quote Originally Posted by ZuK
    As far as I know * is not a sequence point it is possible that either ++a or a++ is evaluated first.
    No.. that would be unspecified behaviour (unspecified is not exactly undefined in the standards terms). Order of evaluation is unspecified. And that to the OP's code causes undefined behaviour because it is only defined that the side-effects of the increments (updating the variable storage included) will happen at a sequence point (here the semi-colon ; ). The evaluations of sub-expressions for those expressions could be interlaced with each other. Which means for this statement - int b = a++ * ++a, the following can happen in any order (events *a interfering with *b):

    1a. read a for a++
    2a. evaluate a++
    3a. update storage of a after a++

    1b. read a for ++a
    2b. evaluate ++a
    3b. update storage of a after ++a

    Those operations themselves in their sub-expressions may be evaluated in order 1, 2, 3 but there can interference with each other. A write and a read or a write and a write without a sequence point causes undefined behaviour as per the standards. Hence, even a = a++; is undefined (two writes to the same variable without a sequence point in between).

  12. #12
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: A question for C++ expert on ++a and a++

    Quote Originally Posted by TheCPUWizard
    btw: This is one of the reasons to NEVER overload operators "&&" and "||". By specification the native versions "short-curcuit", and any overload will evaluate BOTH!!!
    No, this cannot be the reason as any overload would be a function call and function call is a sequence point. The result would be unspecified but not undefined.

  13. #13
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: A question for C++ expert on ++a and a++

    Quote Originally Posted by tzimis
    First of all is good to return 0 because you use int main() and a value must be returned.++a increase the a by 1 and then use the new value of a in the expression.a++ use the current value in the expression and then increase the a by 1
    Did you read any of the posts in this thread above before posting?

    //Btw a similar issue came up on clc++m recently - http://groups.google.co.uk/group/com...b9275faa0ec230

  14. #14
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: A question for C++ expert on ++a and a++

    Quote Originally Posted by exterminator
    No, this cannot be the reason as any overload would be a function call and function call is a sequence point. The result would be unspecified but not undefined.
    If IS correct because an overload IS a function call. Both side would be evaluated. However the built in's are NOT implemented as function calls. Therefore you can do
    Code:
    if ((ptr) && (ptr->MethodIO))
    {
    }
    If the ptr is NULL, then the evauation stops, and there is no (illegal) attempt to call Method1 on the (null) pointer!
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  15. #15
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Re: A question for C++ expert on ++a and a++

    I never thought of that before, but it does make sense that overloaded && and || cannot be 'lazy'.
    That is indeed a good reason not to overload them, though I've never seen any point overloading them anyway.
    My hobby projects:
    www.rclsoftware.org.uk

Page 1 of 2 12 LastLast

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