I’m using a vector in my program like this:
vector<vector<string> > values;
values[0].push_back("test words");
when i run the code, it shows “segment fault”
then i try to gdb it and i got this:
Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_INVALID_ADDRESS at address: 0x0000000000000008
0x00000001000035be in std::vector<std::string, std::allocator<std::string> >::push_back (this=0x0, __x=@0x7fff5fbff330) at stl_vector.h:602
602 if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage)
What is going on here ?
There are no elements in
values, which means this results in out of bounds access:You need to
push_back()an emptyvector<string>intovalues:or create
valueswith at least one entry (see vector constructors):