I have a vector of vector built in the following way:
std::vector<std::vector<int>> symbols;
I want to insert some int in the structure. To do this, I want to check that the position in which the int will be inserted is legal. I'm trying to make the vector growing up in a dynamic way. For this reason, I wrote the following code:
void checkIndex(unsigned int i0, unsigned int i1) {
int mult_fac=2;
if(i0>=symbols.capacity()-1){
symbols.reserve((i0+1)*mult_fac);
}
if(i1>=symbols[i0].capacity()-1){
symbols[i0].reserve((i1 + 1)*mult_fac);
}
}
To verify that all of this works, I wrote a main:
int main() {
for(;;){
int i=rand()%1000;
int j=rand()%1000;
checkIndex(i,j);
}
return 0;}
But, the output shows a bad memory access:
"Process finished with exit code -1073741819 (0xC0000005)"
I'm using Windows 10. I can't understand how to fix it! Thanks for helping me.