Consider this code
#include <vector>
#include <iostream>
main()
{
std::vector <int> x(1);
for(int q=0;; q++)
{
int y = x[q];
std::cout << q << " ";
}
}
On my system with g++, this prints up to 32570 before crashing with Segmentation fault (core dumped)
. Presumably, It takes that long because ~32570 is the size of the smallest chunk of memory that the OS and/or allocator will allocate to the vector. But of course, any time we do an operation like this past the end of the array is normally a bug. So it would be nice if I could have the program exit with a helpful error message whenever this occurs.
A few years ago with Xcode I recall that behavior occurring where it would exit with the type (and name?) of the std::vector
. Currently I'm on Linux with g++. Is there any solution along these lines (or something else)?