Cout stack, operator <operator overload output Problem

Source: Internet
Author: User

In the output stream of cout in C ++, some problems are prone to errors. For example, the following simple program seems simple, but it is a question worth pondering ~~

# Include <iostream>

Using namespace STD;


Int Foo (Int & X)
{
Cout <"no." <x <"call" <"";
Return ++ X;
}

Int main ()
{
Int I = 1;
Cout <Foo (I) <'\ t' <Foo (I) <Endl;

}


At first, many people thought that the output results would be:

1st calls 2 2nd calls 3


But the result is:

1st calls 2nd calls 3 2


Why ??? The first thing you need to know is:


Process space:

Code zone -------------- stores Program Execution Code
Global data zone ------------ stores global data, constants, text volumes, static global volumes, and static local volumes.
Heap zone ---------------- stores dynamic memory for random application by the program
Stack zone ---------------- function data zone (local data zone)
(The above is the memory layout in the running state)


The entire function call process is the stack space operation process. When calling a function, C ++ does the following:
(1) Create the stack space of the called function, and its size is determined by the data volume in the function definition body;
(2) Protect the running status and return address of the called function;
(3) passing parameters;
(4) transfer control to the called function;
(5) After the function is run, copy the result to the bottom of the local data block of the function;
(6) restore the running status of the called function;
(7) return the call function.

Calling a function can be seen as a stack operation for the elements in a stack.


With the above introduction, you can understand the explanation process:

Original output statement: cout <Foo (I) <'\ t' <Foo (I) <Endl;

The execution sequence of cout output is from right to left. So:

1: Execute <Endl, because there is no STD: cout object, unable to execute, pressure Stack

2: Execute <Foo (I), execute Foo (1), output: 1st calls, get the return value 2, or because there is no STD: cout object, it cannot be output immediately, stack pressure
The Code becomes cout <Foo (I) <'\ t' <2 <Endl;

3: Execute <'\ t', without STD: cout object, cannot output immediately, press the stack
The Code becomes cout <Foo (I) <'\ t' <2 <Endl;

4: Execute <Foo (I), execute Foo (2), and output: 3 is returned for 2nd calls, or because no STD: cout object exists, it cannot be output immediately.
The Code becomes cout <3 <'\ t' <2 <Endl;


Pay attention to the following two points for the above process:

1. [Int & X] That is to say, X is actually the alias of I.

2. cout <Foo (I) <'\ t' <Foo (I) <Endl;
<==> Operator <(cout, Foo (I) return values, '\ t', Foo (I) return values, Endl ); the parameters are pushed to the stack area from right to left:


Cout stack, operator <operator overload output Problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.