line with cout countaining text and function-is execution SPLIT???

Mar 13, 2014 at 1:05pm
Hello!

Sorry for bothering, but this doesn't let me sleep in piece!

In this code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include<iostream>
using namespace std;

int* po(int b){
  int d1;
  d1=3*b;
  int d2;
  d2=d1+5;
  int d3;
  d3=d2+3;
  int* a=new int[3];


  //int a[3]={d1,d2,d2+3};

  a[0] = d1;
  a[1] = d2;
  a[2] = d3;


  cout<<"Adress of the array a is: "<<&a[0]<<endl<<endl<<endl;
return a;
}


int main(){
  cout<<"Now main starts"<<endl;
  int st=5;
  int * z;
  z=po(st);

  cout<<endl<<endl;
  cout<<z<<endl<<&z<<endl<<endl;

  cout<<"text1: "<<po(st)<<" TEXT2: "<<endl;
  cout<<z[0]<<" "<<z[1]<<" "<<z[2]<<endl;
  cout<<&z[0]<<" "<<&z[1]<<" "<<&z[2]<<endl;


return 0;
}
 



Output:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Now main starts
Adress of the array a is: 0x8051438




0x8051438
0xbfa92974

Adress of the array a is: 0x80514f0


text1:  0x80514f0 TEXT2: 
15 20 23
0x8051438 0x805143c 0x8051440
 
 



NOW, let' look what is happening in the "magic" line 32:

cout<<"text1: "<<po(st)<<" TEXT2: "<<endl;

it does not start with writing : text1.
It startst with exwcuting po(st), isn't it?

THEN, it BREAKS it, before it came to return a.
THEN, cout<<"text1: " is executed.
THEN, it comes back to po(st), writting just what this function returns .
// IN THE END it couts "TEXT2".

It means, execution fo po(st) is SPLIT into 2 parts:
first part is done first as first task in the line, although it si not written as first.
Second part is executed on that place, where it is written.

Huh!
Well, obvioulsy!
Can I ask for some comments, exampels?
MANY THANS!!!
Last edited on Mar 13, 2014 at 1:38pm
Mar 13, 2014 at 1:23pm
Please, include the output from your program.
Mar 13, 2014 at 1:36pm
U were quicker then me:)!
Mar 13, 2014 at 2:18pm
It means, execution fo po(st) is SPLIT into 2 parts:
Nope. Function execution cannot be interrupted in singlethreaded program.
it does not start with writing : text1.
It startst with exwcuting po(st), isn't it?
In your case, yes. But in the generic case your output can be:
text1: Adress of the array a is: 0x80514f0

0x80514f0
C++ standard lists order of argument evaluation as unspecified, meaning that concrete implementation is up to compiler creators. It does not needs to be documented, nor it nedds to be consistent. Most compilers will evaluate function arguments before executing line of buffered outputs.
Mar 13, 2014 at 2:25pm
So, it means, it just depends on compiler, what he woudl respect as a stuff of better importance and so do it before other things form the same line.

MANY THANKS, THAT ANSWERED MY Q!!!
Topic archived. No new replies allowed.