IBM Support

How do I check array bounds?

Question & Answer


Question

How do I use the -qcheck compiler option to check for array bounds problems in VisualAge C++ V6 for AIX? And how do I check static array bounds in XL C/C++ V7.0 for AIX during compile time?

Answer

In VisualAge C++ V6 for AIX, you can request array bounds checking to be performed at run time by using the -qcheck compiler option. The compiler inserts additional code (in the object module) that raises a signal when an attempt to access an array beyond bounds is detected.

$ cat test.c
int main()
{
   int array[10];
   array[20] = 42;
}

$ xlc -qcheck test.c
$ ./a.out
Trace/BPT trap

In XL C/C++ V7 for AIX, the compiler also performs static compile-time array bounds checking. A warning message is emitted when a constant array subscript exceeds the array bounds.

$ xlc test.c
"test.c", line 4.10: 1506-1298 (W) The subscript 20 is out of range. The valid range is 0 to 9.

[{"Product":{"code":"SSJT9L","label":"XL C\/C++"},"Business Unit":{"code":"BU054","label":"Systems w\/TPS"},"Component":"Compiler","Platform":[{"code":"PF002","label":"AIX"}],"Version":"7.0","Edition":"","Line of Business":{"code":"LOB08","label":"Cognitive Systems"}},{"Product":{"code":"SSTJ5T","label":"XL C Enterprise Edition for AIX"},"Business Unit":{"code":"BU054","label":"Systems w\/TPS"},"Component":"Compiler","Platform":[{"code":"PF002","label":"AIX"}],"Version":"7.0","Edition":"","Line of Business":{"code":"LOB08","label":"Cognitive Systems"}}]

Document Information

More support for:
XL C/C++

Software version:
7.0

Operating system(s):
AIX

Document number:
339295

Modified date:
12 October 2022

UID

swg21204905