-qcheck

Category

Error checking and debugging

Pragma equivalent

#pragma options [no]check

Purpose

Generates code that performs certain types of runtime checking.

If a violation is encountered, a runtime error is raised by sending a SIGTRAP signal to the process. Note that the runtime checks may result in slower application execution.

Syntax

Read syntax diagramSkip visual syntax diagram
        .-nocheck------------------------------.   
>>- -q--+-check--+---------------------------+-+---------------><
                 |    .-:------------------. |     
                 |    V .-all------------. | |     
                 '-=----+-bounds---------+-+-'     
                        +-nobounds-------+         
                        +-divzero--------+         
                        +-nodivzero------+         
                        +-nullptr--------+         
                        +-nonullptr------+         
                        +-stackclobber---+         
                        +-nostackclobber-+         
                        +-unset----------+         
                        '-nounset--------'         

Defaults

-qnocheck

Parameters

all
Enables all suboptions.
bounds | nobounds
Performs runtime checking of addresses for subscripting within an object of known size. The index is checked to ensure that it will result in an address that lies within the bounds of the object's storage. A trap will occur if the address does not lie within the bounds of the object.

This suboption has no effect on accesses to a variable length array.

divzero | nodivzero
Performs runtime checking of integer division. A trap will occur if an attempt is made to divide by zero.
nullptr | nonullptr
Performs runtime checking of addresses contained in pointer variables used to reference storage. The address is checked at the point of use; a trap will occur if the value is less than 512.
stackclobber | nostackclobber
Detects stack corruption of nonvolatile registers in the save area in user programs. This type of corruption happens only if any of the nonvolatile registers in the save area of the stack is modified.

If the -qstackprotect option and this suboption are both on, this suboption detects the stack corruption first.

unset | nounset
Checks for automatic variables that are used before they are set. A trap will occur at run time if an automatic variable is not set before it is used.

The -qinitauto option initializes automatic variables. As a result, the -qinitauto option hides uninitialized variables from the -qcheck=unset option.

Specifying the -qcheck option with no suboptions is equivalent to specifying -qcheck=all.

Usage

You can specify the -qcheck option more than once. The suboption settings are accumulated, but the later suboptions override the earlier ones.

You can use the all suboption along with the no... form of one or more of the other options as a filter. For example, using:
xlc myprogram.c -qcheck=all:nonullptr
provides checking for everything except for addresses contained in pointer variables used to reference storage. If you use all with the no... form of the suboptions, all should be the first suboption.

Predefined macros

None.

Examples

The following code example shows the effect of -qcheck=nullptr:bounds:
void func1(int* p) {
  *p = 42;             /* Traps if p is a null pointer */
}
 
void func2(int i) {
  int array[10];
  array[i] = 42;     /* Traps if i is outside range 0 - 9 */
}
The following code example shows the effect of -qcheck=divzero:
void func3(int a, int b) {
  a / b;            /* Traps if b=0  */
}

The following code example shows the effect of -qcheck=stackclobber:

void func4(char *p, int off, int value) {
  *(p+off)=value;
}

int foo() {
  int i;
  char boo[9];
  i=24;                           
  func4(boo, i, 66);
  /* Traps here */
  return 0;                   
}

int main() {
  foo();
} 
Note: The offset is subject to change at different optimization level. When -O2 or lower optimization level is in effect, func4 will clobber the save area of foo because *(p+off) is in the save area.
In function factorial, result is not initialized when n<=1. To detect an uninitialized variable in factorial.c, enter the following command:
xlc -g -O -qcheck=unset factorial.c
factorial.c contains the following code:
int factorial(int n) {
  int result;
 
  if (n > 1) {
    result = n * factorial(n - 1);
  }
 
  return result; /* line 8 */
}
 
int main() {
  int x = factorial(1);
  return x;
} 
The compiler issues the following informational message during compile time and a trap occurs at line 8 during run time:
1500-099: (I) "factorial.c", line 8: "result" might be used before it is set.
Note: If you set -qcheck=unset at noopt, the compiler does not issue informational messages at compile time.