C++ Segfault when returning an empty vector containing vector
I'm having some trouble returning an empty vector which I wasn't
expecting. Can anybody help explain please!
This is the offending section: - I am aware the returns are the same, its
a placeholder for now.
std::vector<std::vector<double> > PerceptronLayer::calculateLayer() {
std::vector<std::vector<double> > result;
if (vPerceptrons.size() == 0) {
return result;
}
return result;
}
If however I make sure that the vector has some values, by setting some
dummy data, the function returns as I would expect.
std::vector<double> val;
val.push_back(1.0);
result.push_back(val);
GDB output:
Program received signal SIGSEGV, Segmentation fault.
0x000000000040ba60 in std::vector<double, std::allocator<double>
>::operator[] (this=0x0, __n=0)
at /usr/include/c++/4.7/bits/stl_vector.h:751
751 { return *(this->_M_impl._M_start + __n); }
I could protect the function by not allowing it to run on empty data, but
I can't help but feel im missing something fundamental here.
Thanks, Joe
No comments:
Post a Comment