Question: ```
#include <iostream>

class C
{
    private:
        int *o;
    public:
        C(int &i) { *o = i;}
        int* get_val() {return o;}
};

int main()
{
    int t = 9;
    C c(t);
    int* p = c.get_val();
    std::cout<<*p<<std::endl;
    int* h = c.get_val();           //seg fault
    std::cout<<*h<<std::endl;

    int f = 25;
    C g(f);
    int* q = g.get_val();           //seg fault
    std::cout<<*q<<std::endl;        

    return 0;
} 

```

There are two objects to the class C : 'c' and 'g'. 
Returning the private variable's pointer works fine for the first time, but, it results in seg fault, when called for the second time. Why is it so?

So, I commented out the second call and tried creating a new object and called tried to return the private variable's pointer. Even though, 'g' is another object of class C, returning it results in segmentation fault. Why is it so?

Answer: 
The constructor C::C(int) doesn't initialize the member variable o. It tries to write through the pointer o via \*o=i, but since o is uninitialized, the results are unpredictable.
In

```
C(int &i) { *o = i;}

```

You have not already allocated space for you pointer before allocation. It should've been :

```
C(int i):o(new int){o=i;}

```

But allocating memory inside class functions requires that you need to systematically apply `delete/delete[]` on them. Well, this is to prevent memory leak. In short you need :

```
C::~C(){
  delete o; // Freeing the memory associated with each object
}

```

Also, I can't think of a use-case for passing the value by reference in your implementation. So I changed `C(int &i)` to `C(int i)`.