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/']
Destructors run **after** the expression in the `return` statement is evaluated. If it was the other way around you'd be in deep trouble:

```
std::string f() {
std::string res = "abcd";
return res;
}

```

This function should return a string object that holds `"abcd"`, not a copy of a string object that has already been destroyed.
> 
> 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

```