将 shared_ptr 与向量一起使用时出现分段错误 [英] Segmentation fault on using shared_ptr with vectors

查看:101
本文介绍了将 shared_ptr 与向量一起使用时出现分段错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

I wish to have a private vector which can be read from different classes even if the object is destroyed. Hence I read about std::shared_ptr

代码在 storeCounterData 函数中出现段错误

The code is giving a segfault in storeCounterData function

预先感谢您的帮助!!!

Thanks in Advance for your help !!!

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;
}

计数器.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

计数器.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;
  }
}

输出

火箭筒~/VECTOR$ ./a.out

Bazooka~/VECTOR$ ./a.out

内部主

分段错误(核心转储)

推荐答案

Counter 的构造函数中,您正在创建一个空 shared_ptr 向量,即没有托管Counter 的对象(见 (1) 此处).

In the Constructor of Counter you are creating a vector of empty shared_ptrs, i.e. there's no managed object of Counter (see (1) here).

只需将行更改为:

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

您正在创建一个指针向量,因此我在这里看到了两个选项,用于您希望它的工作方式:

You are creating a vector of pointers, so I see two options here for the way you want it to work:

  1. 你这样做的方式:用 3 个 shared_ptr 实例化向量,但是你需要检查指针是否已经创建,每当你访问指针时.
  1. The way you did it: Instantiate the vector with 3 shared_ptrs, but then you need to check if the pointer was already created, whenever you access the pointer.

也许这就是要走的路.shared_ptrs 也是指针,你应该这样对待它们并始终检查有效性.

Maybe that's the way to go. shared_ptrs are pointers aswell, you should treat them like that and always check for validity.

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,这样向量中​​就只有有效"(你永远无法确定)shared_ptr.立>
  1. Use std::vector::push_back, so that you only have "valid" (you never can be sure of this) shared_ptr in the vector.

这篇关于将 shared_ptr 与向量一起使用时出现分段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆