Compiling the program below with MSVC 13.10.3077 for 80x86 using the
/GX /GZ /Ge options

#include <iostream>
using namespace std;
int main()
{
int ix[1];
ix[0] = 3;
cout << ix[0] << " " << ix[1] << endl;
}

produces a compilation warning
local variable 'ix' used without having been initialized

and output

3 -858993460

Is there an option that will cause the program to terminate at run
time
(preferably with an informative error message) when trying to access
an out-of-bounds array element?

If I add the line

ix[1] = 4;

before the cout the program prints '3 4' and then crashes. It would be
better if it crashed at the 'ix[1] = 4;' line.

Re: run-time array bounds checking by David

David
Mon Jun 21 17:21:07 CDT 2004

>Compiling the program below with MSVC 13.10.3077 for 80x86 using the
>/GX /GZ /Ge options
>
>#include <iostream>
>using namespace std;
>int main()
>{
> int ix[1];
> ix[0] = 3;
> cout << ix[0] << " " << ix[1] << endl;
>}
>
>produces a compilation warning
>local variable 'ix' used without having been initialized
>
>and output
>
>3 -858993460
>
>Is there an option that will cause the program to terminate at run
>time
>(preferably with an informative error message) when trying to access
>an out-of-bounds array element?

If you use the /RTCu option you should get:

"Run-Time Check Failure #3 - The variable 'ix' is being used without
being defined."

>If I add the line
>
>ix[1] = 4;
>
>before the cout the program prints '3 4' and then crashes. It would be
>better if it crashed at the 'ix[1] = 4;' line.

The /RTCs option can detect the incorrect usage when the function
exits.

... so best to use both of them.

Dave
--
MVP VC++ FAQ: http://www.mvps.org/vcfaq