output question

This is a discussion on output question within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> int& test(int& i) { ++i; return i; } int main() { int i = 0; std::cout << ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    65

    output question

    Code:
    #include <iostream>
    
    int& test(int& i) {
      ++i;
      return i;
    }
    
    int main() {
      int i = 0;
      std::cout << test(i) << ' ' << test(i) << '\n';
    }
    I'm having trouble understanding the output from this program. Can someone explain to me why the output is "2 2" if test returns by value and "2 1" when it returns by reference?

  2. #2
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    Quote Originally Posted by jw232 View Post
    Code:
    #include <iostream>
    
    int& test(int& i) {
      ++i;
      return i;
    }
    
    int main() {
      int i = 0;
      std::cout << test(i) << ' ' << test(i) << '\n';
    }
    I'm having trouble understanding the output from this program. Can someone explain to me why the output is "2 2" if test returns by value and "2 1" when it returns by reference?
    What seems to be happening is you are passing i (which is not a pointer) to your function which is now interpreting it as a reference. What is passed back is actualy the _address_ of i, which is then altered by the second test(i).

    This is simply a pointer mistake, you might want to brush up on them. i DOES at some point have the value 1, however it's adress is changed to 2. The behavior of cout << is not a stream, it sends a whole data buffer rather then data fragments, so what you're actualy saying here is:

    Code:
    std::cout << address of i << ' ' << address of i << '\n';
    Which is why the output is 2,2. If you want to correct this, all you have to do is:
    Code:
    int test(int i) {
      return ++i; //You can turn this into one statement.
    }
    
    int main() {
      int i = 0;
      std::cout << test(i) << ' ' << test(i) << '\n';
    }
    Again, this was an issue with pointers, which you didint need to use.
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Since the output is not as described, there is nothing to explain.

    Edit: I feel the need to mention that no pointers were harmed during the execution of the code, since no pointers exist during the execution of this code. (References != pointers.)

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    65
    I tried a different compiler and I got "2 1" for both cases. I guess it was just my compiler.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by jw232 View Post
    I tried a different compiler and I got "2 1" for both cases. I guess it was just my compiler.
    You ... what? Are we looking at the same code here? Starts at zero, increments inside the function? I'm pretty sure it's not possible to get 2 1 out of either case, let alone both. (Unless this is going to turn out to be one of those trick sequence-point things. Now I have to look up <<.)

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    65
    Have you tried it yourself?
    G++ in Cygwin gives me "2 1" for value, "2 2" for reference
    VC++ gives me "2 1" both cases

    I don't see what you mean, test is called twice, therefore i is incremented twice.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I'm using g++. I get "1 2" and "1 1".

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    OOH! This is fun!
    Using the int & version:
    Code:
    g++ -O3 -o temp.exe temp.cpp
    gives "1 2".
    Code:
    g++ -o temp.exe temp.cpp
    gives "2 2". So it must be one of those sequence-point things involved in the << line. (I hate those things.)
    And using Visual C++ (don't remember the optimization level) I did get "2 1".

    Splitting the line into two statements (thus forcing the sequence point issue) I got the "1\n2" I expected.

    Edit: I would have been less surprised by this had I fully thought through the return of int &, I think. For some reason, I was thinking "int test(int &i)". Oops.
    Last edited by tabstop; 08-11-2008 at 09:36 PM.

  9. #9
    Registered User
    Join Date
    Apr 2006
    Posts
    2,050
    Test is modifying main's version of i two times, and using the result. The code is basically equivalent to this:
    Code:
     std::cout << ++i << ' ' << ++i << '\n';
    You are not allowed to modify a value twice in the same unbroken expression. Nor can a value be retrieved and modified in the same expression, unless it is retrieved to calculate it's new value.

    Sequence points created by operators &&, ||, ?:, and ,(comma) break up an expression such that order across them is defined. Expressions can also be broken into separate lines, thereby being separate expressions entirely. << is not a sequencing operator, so it does not guarantee order, and thus the problem.
    Last edited by King Mir; 08-11-2008 at 09:15 PM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    King Mir got this one off the bat. It's just a dressed up sequence point violation, and thus a bunch of undefined output.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question: Output of data.txt from the following program
    By outlaw525 in forum C Programming
    Replies: 9
    Last Post: 06-23-2008, 03:33 PM
  2. strange virtual function output
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 02-08-2008, 07:08 AM
  3. Basic C input output program help
    By trevordunstan in forum C Programming
    Replies: 2
    Last Post: 01-27-2008, 05:41 PM
  4. a question about file output
    By wang8442 in forum C Programming
    Replies: 3
    Last Post: 12-26-2004, 02:18 AM
  5. a newbie question about file output
    By maybe in forum C Programming
    Replies: 8
    Last Post: 10-12-2004, 08:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21