/RTC (Run-Time Error Checks)
/RTC1 /RTCc /RTCs /RTCu
Run-time error checks are a way for you to find problems in your running code; see Using Native Run-Time Checks for a full description of this feature. The run-time error checks feature is enabled and disabled by the /RTC group of compiler options and the runtime_checks pragma.
| Option | Description |
|---|---|
| /RTC1 | Equivalent of /RTCsu. |
| /RTCc | Reports when a value is assigned to a smaller data type that results in a data loss. For example, if a value of type short 0x101 is assigned to a variable of type char.
This option will report situations where you intend to truncate, as in a situation where you want the first eight bits of an int returned as a char. Since /RTCc will cause a run-time error if any information will be lost as a result of the assignment, you can mask off the information you need to avoid a run-time error as a result of /RTCc. For example:
#include <crtdbg.h>
char get8bits(int value, int position)
{
_ASSERT(position < 32);
return (char)(value >> position);
// try the following line instead
// return (char)((value >> position) && 0xff);
}
int main()
{
get8bits(12341235,3);
}
|
| /RTCs | Enables stack frame run-time error checking:
|
| /RTCu | Reports when a variable is used without having been initialized. For example, an instruction that generates C4701 may also generate a run-time error under /RTCu. Any instruction that generates C4700 will generate a run-time error under /RTCu.
However, consider the following code fragment: int a, *b, c; if ( 1 ) b = &a; c = a; // no run-time error with /RTCu If a variable could have been initialized, it will not be reported at run time by /RTCu. For example, after a variable is aliased through a pointer, the compiler will not track the variable and report uninitialized uses. In effect, you can initialize a variable by taking its address. The & operator works like an assignment operator in this situation. |
Note If you compile your program at the command line using any of the /RTC compiler options, any pragma optimize instructions in your code will silently fail. This is because run-time error checks are not valid in a release (optimized) build.
The __MSVC_RUNTIME_CHECKS preprocessor directive will be defined when you use any /RTC option or /GZ.
To set this compiler option in the Visual Studio development environment
- Open the project's Property Pages dialog box. For details, see Setting Visual C++ Project Properties.
- Click the C/C++ folder.
- Click the Code Generation property page.
- Modify one or both of the following properties: Basic Runtime Checks or Smaller Type Check.
To set this compiler option programmatically
See BasicRuntimeChecks and SmallerTypeCheck properties.