When I use clear() on a std::vector, it is supposed to destroy all the elements in the vector, but instead it doesn't.

Sample code:

vector<double> temp1(4);
cout << temp1.size() << std::endl;
temp1.clear();
cout << temp1.size() << std::endl;

temp1[2] = 343.5; // I should get segmentation fault here ....

cout << "Printing..... " << temp1[2] << endl;
cout << temp1.size() << std::endl;

Now, I should have gotten segmentation fault while trying to access the cleared vector, but instead it fills in the value there (which according to me is very buggy)

Result looks as follows:

4
0
Printing..... 343.5
0

Is this normal? This is a very hard bug to spot, which basically killed my code for months.

Edit
Report