title: Segmentation fault on using shared_ptr with vectors
body: <p>I am using a shared_ptr for the first time pardon me If I have made a very silly mistake and help me in overcoming this segmentation fault.</p>
  
  <p>I wish to have a private <code>vector</code> which can be read from different classes even if the object is destroyed. Hence I read about <code>std::shared_ptr</code></p>
  
  <p>The code is giving a segfault in <code>storeCounterData</code> function</p>
  
  <p>Thanks in Advance for your help !!!</p>
  
  <p>main.cpp </p>
  
  <pre><code>#include &lt;iostream&gt;
  #include "counter.hpp"
  #include "getCounter.hpp"
  
  const int max_ports = 3;
  
  int main()
  {
    Counter *counter = new Counter(3);
    counter-&gt;age = 1;
    counter-&gt;id = 12;
    counter-&gt;pincode = 123;
  
    std::vector&lt;Counter*&gt; counterData;
  
    std::cout&lt;&lt;"inside main"&lt;&lt;std::endl;
  
    counter-&gt;storeCounterData(counter,0);
    counter-&gt;storeCounterData(counter,1);
    counter-&gt;storeCounterData(counter,2);
  
    std::cout&lt;&lt;"inside main calling getCounterData"&lt;&lt;std::endl;
    counter-&gt;getCounterData(counterData);
  
    Counter countji(3);
  
    countji.getCounterData(counterData);
  
    //getCounterData class function
  
     getCounter *gcount = new getCounter();
  
     gcount-&gt;printCounterData();
  
    return 0;
  }
  </code></pre>
  
  <p>Counter.hpp </p>
  
  <pre><code>#ifndef COUNTERHPP
  #define COUNTERHPP
  #include &lt;vector&gt;
  #include &lt;memory&gt;
  
  class Counter
  {
  private:
  typedef std::shared_ptr&lt;Counter&gt; sharedCtr; 
  std::vector&lt;sharedCtr&gt; vecData;
  public:
  Counter();
  Counter(int vecSize);
  int age, id, pincode;
  void storeCounterData(Counter *counter,int user);
  void getCounterData(std::vector&lt;Counter*&gt; &amp;counter);
  
  };
  
  #endif
  </code></pre>
  
  <p>Counter.cpp </p>
  
  <pre><code>#include "counter.hpp"
  #include &lt;iostream&gt;
  #include &lt;vector&gt;
  
  Counter::Counter()
  {
  
  }
  
  Counter::Counter(int vecSize):vecData(vecSize)
  {
  
  }
  
  void Counter::storeCounterData(Counter *counter,int user)
  { 
    vecData[user]-&gt;age = counter-&gt;age;
    vecData[user]-&gt;id = counter-&gt;id;
    vecData[user]-&gt;pincode = counter-&gt;pincode;
  
    std::cout&lt;&lt;"vector size inside storeCounterData = "&lt;&lt;vecData.size()&lt;&lt;std::endl;
  }
  
  void Counter::getCounterData(std::vector&lt;Counter*&gt; &amp;counter)
  {
  
    std::cout&lt;&lt;"vector size inside getCounterData = "&lt;&lt;vecData.size()&lt;&lt;std::endl;
  
  for (auto&amp; c : vecData)
    {
      std::cout &lt;&lt; c-&gt;age &lt;&lt; std::endl;
      std::cout &lt;&lt; c-&gt;id &lt;&lt; std::endl;
      std::cout &lt;&lt; c-&gt;pincode &lt;&lt; std::endl;
    }
  }
  </code></pre>
  
  <p>Output</p>
  
  <blockquote>
    <p>Bazooka~/VECTOR$ ./a.out </p>
    
    <p>inside main</p>
    
    <p>Segmentation fault (core dumped)</p>
  </blockquote>