使用带矢量的shared_ptr时出现分段错误

时间:2018-06-03 07:23:48

标签: c++ vector shared-ptr

我第一次使用shared_ptr原谅我如果我犯了一个非常愚蠢的错误并帮助我克服这个分段错误。

我希望有一个私有vector,即使对象被销毁,也可以从不同的类中读取。因此,我读到了std::shared_ptr

代码在storeCounterData函数

中给出了段错误

先谢谢你的帮助!!!

main.cpp

#include <iostream>
#include "counter.hpp"
#include "getCounter.hpp"

const int max_ports = 3;

int main()
{
  Counter *counter = new Counter(3);
  counter->age = 1;
  counter->id = 12;
  counter->pincode = 123;

  std::vector<Counter*> counterData;

  std::cout<<"inside main"<<std::endl;

  counter->storeCounterData(counter,0);
  counter->storeCounterData(counter,1);
  counter->storeCounterData(counter,2);

  std::cout<<"inside main calling getCounterData"<<std::endl;
  counter->getCounterData(counterData);

  Counter countji(3);

  countji.getCounterData(counterData);

  //getCounterData class function

   getCounter *gcount = new getCounter();

   gcount->printCounterData();

  return 0;
}

Counter.hpp

#ifndef COUNTERHPP
#define COUNTERHPP
#include <vector>
#include <memory>

class Counter
{
private:
typedef std::shared_ptr<Counter> sharedCtr; 
std::vector<sharedCtr> vecData;
public:
Counter();
Counter(int vecSize);
int age, id, pincode;
void storeCounterData(Counter *counter,int user);
void getCounterData(std::vector<Counter*> &counter);

};

#endif

Counter.cpp

#include "counter.hpp"
#include <iostream>
#include <vector>

Counter::Counter()
{

}

Counter::Counter(int vecSize):vecData(vecSize)
{

}

void Counter::storeCounterData(Counter *counter,int user)
{ 
  vecData[user]->age = counter->age;
  vecData[user]->id = counter->id;
  vecData[user]->pincode = counter->pincode;

  std::cout<<"vector size inside storeCounterData = "<<vecData.size()<<std::endl;
}

void Counter::getCounterData(std::vector<Counter*> &counter)
{

  std::cout<<"vector size inside getCounterData = "<<vecData.size()<<std::endl;

for (auto& c : vecData)
  {
    std::cout << c->age << std::endl;
    std::cout << c->id << std::endl;
    std::cout << c->pincode << std::endl;
  }
}

输出

  

Bazooka~ / VECTOR $ ./a.out

     

在主

内      

分段错误(核心转储)

1 个答案:

答案 0 :(得分:2)

Counter的构造函数中,您正在创建一个空shared_ptr的向量,即没有Countersee (1) here)的托管对象。< / p>

只需将该行更改为:

Counter(int vecSize) : vecData(vecSize, std::make_shared<Counter>()) {}

编辑: 你正在创建一个指针向量,所以我在这里看到两个选项,你想要它的工作方式:

  1. 你这样做的方式:用3 shared_ptr s实例化向量,但是每当你访问指针时都需要检查指针是否已经创建。
  2. 也许这就是你要走的路。 shared_ptr也是指针,你应该像那样对待它们并且总是检查它们的有效性。

    Counter(int vecSize) : vecData(vecSize) {} // previous constructor
    
    void storeCounterData(Counter* counter, int user) {
      if (!vecData[user]) {
        vecData[user] = std::make_shared<Counter>();
      }
      vecData[user]->age = counter->age;
      vecData[user]->id = counter->id;
      vecData[user]->pincode = counter->pincode;
    
      std::cout << "vector size inside storeCounterData = " << vecData.size()
                << std::endl;
    }
    void getCounterData(std::vector<Counter*>& counter) {
      std::cout << "vector size inside getCounterData = " << vecData.size()
                << std::endl;
    
      for (auto& c : vecData) {
        if (c) {
          std::cout << c->age << std::endl;
          std::cout << c->id << std::endl;
          std::cout << c->pincode << std::endl;
        }
      }
    }
    
    1. 使用std::vector::push_back,以便您只有&#34;有效&#34; (你永远无法确定这一点)向量中的shared_ptr