对向量使用 shared_ptr 时出现分段错误
Segmentation fault on using shared_ptr with vectors
我是第一次使用 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
inside main
Segmentation fault (core dumped)
在 Counter
的构造函数中,您正在创建一个空 shared_ptr
向量,即没有 Counter
(see (1) here) 的托管对象。
只需将行更改为:
Counter(int vecSize) : vecData(vecSize, std::make_shared<Counter>()) {}
编辑:
你正在创建一个指针向量,所以我在这里看到两个选项来满足你希望它的工作方式:
- 你做的方式:用 3
shared_ptr
s 实例化向量,但是你需要检查指针是否已经创建,每当你访问指针时。
也许这就是要走的路。 shared_ptr
s 也是指针,你应该这样对待它们并始终检查有效性。
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;
}
}
}
- 使用
std::vector::push_back
,这样向量中只有"valid"(你永远无法确定这一点)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
inside main
Segmentation fault (core dumped)
在 Counter
的构造函数中,您正在创建一个空 shared_ptr
向量,即没有 Counter
(see (1) here) 的托管对象。
只需将行更改为:
Counter(int vecSize) : vecData(vecSize, std::make_shared<Counter>()) {}
编辑: 你正在创建一个指针向量,所以我在这里看到两个选项来满足你希望它的工作方式:
- 你做的方式:用 3
shared_ptr
s 实例化向量,但是你需要检查指针是否已经创建,每当你访问指针时。
也许这就是要走的路。 shared_ptr
s 也是指针,你应该这样对待它们并始终检查有效性。
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;
}
}
}
- 使用
std::vector::push_back
,这样向量中只有"valid"(你永远无法确定这一点)shared_ptr
。