and so on. The check can create significant run-time performance overhead. See +check=uninit and +check=malloc for their interaction with this option.

all - Enables out-of-bounds checks for both arrays and pointers. This is equal to

+check=bounds:array +check=bounds:pointer.

none - Disables out-of-bounds checks.

+check=bounds (with no suboption) is equal to +check=bounds:array. This may change in the future to also include +check=bounds:pointer.

When +check=all is specified, it enables +check=bounds:array only. To enable the pointer out-of-bounds check, you must explicitly specify +check=bounds:pointer.

You can combine +check=bounds:[pointer all] with all other +check options, except for +check=globals (which would be ignored in this case).

Also see the +check=malloc and the +check=stack options for related runtime checks for heap and stack objects.

Example:

This example uses +check=bounds:pointer to find a program bug:

$ cat rttest3.c

1 #include <stdio.h>

2#include <memory.h>

3#include <stdlib.h>

45 int a[10];

6 char b[10];

7 int *ip = &a[0]; // points to global array 8

9int i;

11void *foo(int n)

12{

13return malloc(n * sizeof(int));

14}

16int main(int argc, char **argv)

17{

18

int

j;

// uninitialized

variable

19

 

 

 

 

20

int

*lp = (int*)foo(10);

// points to heap object

21

 

 

 

 

22// out of bound if "a.out 10"

23if (argc > 1) {

24i = atoi(argv[1]);

25}

26

27memset(b, 'a', i);

29lp[i] = i;

31ip[i+1] = i+1;

33printf("lp[%d]=%d, ip[%d]=%d, ip[j=%d]=%d\n",

34i, lp[i], i+1, ip[i+1], j, ip[j]);

36return 0;

37}

Compiling with +check=bounds:pointer:

$ cc +check=bounds:pointer rttest3.c

"rttest3.c", line 34: warning #2549-D: variable "j" is used before its value is set

76 Command-Line Options

Page 76
Image 76
HP C/aC++ for PA-RISC Software manual +check=boundsarray +check=boundspointer, Compiling with +check=boundspointer