For raw arrays, I don't think so, because -fbounds-check
didn't work with your example and MingW g++ 4.4.1, and because the old 3.x docs I have say
-fbounds-check
For front-ends that support it,
generate additional code to check that
indices used to access arrays are
within the declared range. This is
currently only supported by the Java
and Fortran 77 front-ends, where this
option defaults to true and false
respectively.
However, with std::vector
you can use at
to have a slightly impractical run-time bounds-checking (generates exception). And you can use a special debug version of the standard library, that provides practical run-time bounds-checking for []
. For example, when compiling…
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> arr( 2 );
cout << arr[ 4 ] << endl;
}
… you get different respectively non-checking and checking behavior for the release and debug versions of the g++ standard library implementation:
C:\test> g++ x.cpp & a
4083049
C:\test> g++ x.cpp -D _GLIBCXX_DEBUG -D _GLIBCXX_DEBUG_PEDANTIC & a
c:\program files\mingw\bin\../lib/gcc/mingw32/4.4.1/include/c++/debug/vector:265:
error: attempt to subscript container with out-of-bounds index 4, but
container only holds 2 elements.
Objects involved in the operation:
sequence "this" @ 0x0x22ff1c {
type = NSt7__debug6vectorIiSaIiEEE;
}
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
C:\test> _
Reportedly for newer g++ versions (after 4.0) you don't need the _GLIBCXX_DEBUG_PEDANTIC
symbol. For details, see the GNU documentation.
Cheers & hth.,
-Wall -Wextra -ansi -pedantic
don't generate warnings for this program :( – Grosso