在指向矢量的智能指针上使用push_back()时发生运行时错误

时间:2019-04-18 18:04:34

标签: c++ pointers vector runtime-error

我正在尝试通过取消引用智能指针来填充向量。在运行期间,程序会在用于输入变量输入的第一个“ for”循环的一次迭代之后崩溃。

final_simplex

使用VS调试器时,它表明已引发异常(引发异常:读取访问冲突。 向量文件中的std :: _ Vector_alloc>> :: _ Myend(...)返回0xC。),特别是1793年至1795年的行:

using namespace std;

class Measurement
{
protected:
    int sample_size;
    string label;
    shared_ptr <vector<double>> data;
public:
    // parameterised constructor
    Measurement(string pLabel, int pSample_size)
    {
        label = pLabel;
        sample_size = pSample_size;
        cout << "Please input your dataset one entry at a time:" << endl;
        for (int i = 0; i < sample_size; i++)
        {
            double input;
            cin >> input;
            data->push_back(input); // NOT WORKING???
        }
    }
};

int main()
{
    Measurement A("xData", 5);
    return 0;
}

此错误的原因是什么?

3 个答案:

答案 0 :(得分:2)

默认构造的shared_ptr指向无效的内容。来自https://en.cppreference.com/w/cpp/memory/shared_ptr/shared_ptr

  

构造一个没有托管对象(即空shared_ptr)的shared_ptr。

您需要对其进行初始化,以使其指向它管理的有效对象,然后才能使用基础指针。例如,将构造函数更改为:

Measurement(string pLabel, int pSample_size) : data(new std::vector<double>()) 
{
   ...
}

Measurement(string pLabel, int pSample_size) : data(std::make_shared<std::vector<double>>()) 
{
   ...
}

答案 1 :(得分:1)

在使用之前,您需要为data分配内存:

Measurement(string pLabel, int pSample_size) {
   ...
   data = std::make_shared<vector<double>>();
   ...
}

答案 2 :(得分:0)

您从未初始化过ptr。下面演示了default initializers for member variablesmember initializer lists的用法。

您可以轻松地将ptr初始化添加到初始化列表中,但是由于它不依赖于任何构造函数参数。最好以以下方式声明其构造,以避免在创建其他构造函数时出现复制/粘贴错误。

#include <iostream>                                                              
#include <vector>                                                                
#include <memory>                                                                

using namespace std;                                                             

class Measurement {                                                              
  protected:                                                                     
    int                        sample_size_;                                     
    string                     label_;                                           
    shared_ptr<vector<double>> data_{make_shared<vector<double>>()};             

  public:                                                                        
    // parameterised constructor                                                 
    Measurement( string pLabel, int pSample_size )                               
      : sample_size_( pSample_size )                                             
      , label_( pLabel )                                                         
    {                                                                            
        cout << "Please input your dataset one entry at a time:" << endl;        
        for ( int i = 0; i < sample_size_; i++ ) {                               
            double input;                                                        
            cin >> input;                                                        
            data_->push_back( input ); // NOT WORKING???                         
        }                                                                        
    }                                                                            

    friend ostream& operator<<( ostream& os, Measurement const& op1 )            
    {                                                                            
        for ( auto& v : *op1.data_ )                                             
            os << v << " ";                                                      
        return os;                                                               
    }                                                                            
};                                                                               

int main()                                                                       
{                                                                                
    Measurement A( "xData", 5 );                                                 
    cout << A << endl;                                                           
    return 0;                                                                    
}  

输出:

g++     example.cpp   -o example
Please input your dataset one entry at a time:
1
2
3
4
5
1 2 3 4 5 
相关问题