My question is pretty straightforward.
Why is it so that while iterating over an “EMPTY” vector using “for” loop leads to a runtime error.
A simple example would be:
vector<int> v;
cout << v.size() << endl;
for( int i = 0; i < v.size(); i++ )
cout << v[i] << " ";
The thing is even though “v.size()” returns a 0, a SIGSEGV error occurs.
This is strange in the fact that the for loop should never be executed so there is no question about accessing anything out of the bounds.
I know this can be easily resolved by using vector<int>::iterator, but still why’s this problem with for loop ?
I have been using vector and stl based container from a long time but i have not come across any such situation throughout the period. I am damm sure you must be something wrong… else this is perfectly valid and works accordingly …