I saw the following program on a site and I cannot understand the output . 
<http://codepad.org/T0qblfYg>

```
#include <iostream>
using namespace std;

int i;

class A
{
public:
    ~A()
    {
        i=10;
    }
};

int foo()
{
    i=3;
    A ob;
    return i;
}

int main()
{
    cout <<"\n 1) Before calling i is "<<i;
    cout <<"\n 2) i = "<<i << " & the  function return value = " 
            << foo() << " and now i  is "   << i << endl;

    return 0;
}

```

output is :

```
 1) Before calling i is 0
 2) i = 10 & the  function return value = 3 and now i  is 10

```

Now i is a global variable and destruction of A should have changed it to 10 before the call was returned to main. It is suggested that destruction of A happens after the call is returned to main but as a caller when i call a function i always expect the result to be final . Here the function return value is not 10 but 3 . 

My question is why did i see this and where exactly is destruction getting called .
['https://Stackoverflow.com/questions/34569249', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1636300/']
The order of evaluation is not what you would expect. When calling functions, the arguments to the functions can be evaluated in any order and even interleaved. So if you call

```
f( g(), h(), i() );

```

the order in which `g()`, `h()` and `i()` are called is up to the compiler. *This also applies to operators.* When you have an expression of the form 

```
expr1 << expr2 << expr3 << expr4;

```

the order in which the expressions are evaluated is arbitrary. You only know, that the `operator<<` calls will be evaluated from left to right, because the operator is left-to right associative and you could rewrite the expression as

```
((expr1 << expr2) << expr3) << expr4;

```

The syntax tree looks like that:

```
              <<
            /    \
         <<     expr4
       /    \
    <<     expr3
  /    \
expr1 expr2

```

The leafs of the syntax tree can be evaluated in any order, even interleaved. And after all the leafs of an operator or function call are evaluated, the function itself will be called. 

In your case `foo()` seems to get called before `i` gets printed out the first time and it is perfectly valid (though unexpected) behaviour.
> 
> why did i see this and where exactly is destruction getting called .
> 
> 
> 

I believe the instance destructor is called when that instance goes out of scope. You might test it the following way (by controlling the scope of the instance -- see bar() below)

Note - to reduce confusion, I 1) renamed the global and 2) explicitly controlled when the foo or bar is called w.r.t. subsequent cout.

```
int g; // uninitialized!

class A
{
public:
   ~A()
      {
         g=10;
      }
};

int foo()
{
   g=3;
   A ob;
   return g;
} // <<<<<<<<<<< ob destructor called here

int bar()
{
   g=3;
   {
      A ob;
   }  // <<<<<<<<<<< ob destructor called here
   return g;
}

int t272(void)
{
   g = 0;  // force to 0
   std::cout << "\n 1) Before calling foo g is " << g;

   int fooRetVal = foo(); // control when called
                          // do not leave up to compiler

   std::cout << "\n 2) after foo, g = " << g
             << "\n 3)    fooRetVal = " << fooRetVal
             << "\n 4)    and now g = " << g
             << std::endl;

   g = 0;  // force to 0
   std::cout << "\n 1) Before calling bar g is " << g;

   int barRetVal = bar(); // control when called
                          // do not leave up to compiler

   std::cout << "\n 2) after bar, g = " << g
             << "\n 3)    barRetVal = " << barRetVal
             << "\n 4)    and now g = " << g
             << std::endl;

   return (0);
}

```

With the output

```
1) Before calling foo g is 0
2) after foo, g = 10
3)    fooRetVal = 3     <<< dtor called after return of foo
4)    and now g = 10

1) Before calling bar g is 0
2) after bar, g = 10
3)    barRetVal = 10     <<< dtor was called before return of bar
4)    and now g = 10

```