In a C++ program I write:
#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<int> a;
a.resize(1);
for( int i = 0 ; i < 10 ; i++ ) {
cout << a[i] << " ";
}
return 0;
}
this program prints the correct value of a[0] (because it is allocated) but also prints values at the rest of the 10 locations instead of giving a segmentation fault.
How to overcome this while writing your code? This causes problems when your code computes something and happily accesses memory not meant to be accessed.