Digital said:
In Java, accessing an array beyond its allocated length generates
an exception.
But not a segmentation fault.
Since on my system (linux w/g++) accessing an
array beyond its allocation generally results in a seg fault,
Sorry, but that's not true. What causes a segmentation fault is
accessing a piece of memory that's restricted (either not mapped,
or mapped for read only and you are trying to write it). Since
memory is allocated in chunks, and array allocations are interleaved
with other memory objects, the chances are unless you have a totally
wild subscript (much larger than the end), you won't get a segmentation
fault directly from the access.
As others pointed out C (and hence by inheritance C++) does no range
checking on it's arrays. You can use a different object in C++
(such as vector) to provide that. There are also packages out there
that instrument C++ in certain ways to do the check (at a substantial
performance penalty) to do these checks.
Your initial premise is wrong (that an array index error necessarily
generates a segmentation fault in C++) and there is no way to detect
either one.