I was working with 3D vectors and everything worked perfectly. When I added and worked with ofstream files Segmentation Fault appeared. I don't understand the problem at all. The following code doesn't work:
#include <iostream>
#include <vector>
#include <fstream>
std::vector < std::vector < std::vector <float> > > hand;
int main(){
//First Part
std::ofstream file;
file.open("test.csv");
file << "Hello World!";
file.close();
//Second Part
hand.reserve(20);
for (int i=0; i<hand.capacity(); i++){
hand[i].reserve(4);
}
return 0;
}
If you comment one of the parts the code will work perfectly. The Segmentation fault appears when you want to work with them at the same time.
It is also important to notice that the code can work if instead of:
hand.reserve(20);
we use a number below 8:
hand.reserve(7); //or lower
My question is: Why is the code not working when I use them at the same time? What can I do to fix it? Do you have any explanation about this particular case?
I'll have to implement this in a much bigger code, so it would be good to know the root cause and avoid it in the next cases.