I am trying to fill a vector, by dereferencing a smart-pointer. During runtime the program crashes after one iteration of the first "for" loop used for entering the variable input.
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;
}
When using the VS debugger it shows that an exception is thrown (Exception thrown: read access violation. std::_Vector_alloc > >::_Myend(...) returned 0xC.) in the vector file, specifically lines 1793 - 1795:
bool _Has_unused_capacity() const _NOEXCEPT
{ // micro-optimization for capacity() != size()
return (this->_Myend() != this->_Mylast());
What is the cause of this error?
datapointing to???nullptr(also true for smart pointers) is Undefined Behaviour.