Redigera

Dela via


/RTC (Run-time error checks)

Used to enable and disable the run-time error checks feature, in conjunction with the runtime_checks pragma.

Syntax

/RTC1
/RTCc
/RTCs
/RTCu

Arguments

/RTC1
Equivalent to /RTCsu.

/RTCc
Reports when a value is assigned to a smaller data type and results in a data loss. For example, it reports if a short type value of 0x0101 is assigned to a variable of type char.

This option can report situations in which you intend to truncate. For example, when you want the first 8 bits of an int returned as a char. Because /RTCc causes a run-time error if an assignment causes any information loss, first mask off the information you need to avoid the run-time error. For example:

#include <crtdbg.h>

char get8bits(unsigned 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);
}

Because /RTCc rejects code that conforms to the standard, it's not supported by the C++ Standard Library. Code that uses /RTCc and the C++ Standard Library may cause compiler error C1189. You can define _ALLOW_RTCc_IN_STL to silence the warning and use the /RTCc option.

/RTCs
Enables stack frame run-time error checking, as follows:

  • Initialization of local variables to a nonzero value. This option helps identify bugs that don't appear when running in debug mode. There's a greater chance that stack variables still have a zero value in a debug build compared to a release build. That's because of compiler optimizations of stack variables in a release build. Once a program has used an area of its stack, it's never reset to 0 by the compiler. That means any uninitialized stack variables that happen to use the same stack area later can return values left over from the earlier use of this stack memory.

  • Detection of overruns and underruns of local variables such as arrays. /RTCs doesn't detect overruns when accessing memory that results from compiler padding within a structure. Padding could occur by using align, /Zp (Struct Member Alignment), or pack, or if you order structure elements in such a way as to require the compiler to add padding.

  • Stack pointer verification, which detects stack pointer corruption. Stack pointer corruption can be caused by a calling convention mismatch. For example, using a function pointer, you call a function in a DLL that is exported as __stdcall but you declare the pointer to the function as __cdecl.

/RTCu
Reports when a variable is used without having been initialized. For example, an instruction that generates warning C4701 may also generate a run-time error under /RTCu. Any instruction that generates Compiler Warning (level 1 and level 4) 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's not reported at run time by /RTCu. For example, after a variable is aliased through a pointer, the compiler doesn't 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.

Remarks

Run-time error checks are a way for you to find problems in your running code; for more information, see How to: Use native run-time checks.

You can specify more than one /RTC option on the command line. The option arguments may be combined; for example, /RTCcu is the same as /RTCc /RTCu.

If you compile your program at the command line using any of the /RTC compiler options, any pragma optimize instructions in your code silently fail. That's because run-time error checks aren't valid in a release (optimized) build.

Use /RTC for development builds; Don't use /RTC for a release build. /RTC can't be used with compiler optimizations (/O Options (Optimize Code)). A program image built with /RTC is slightly larger and slightly slower than an image built with /Od (up to 5 percent slower than an /Od 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

  1. Open the project's Property Pages dialog box. For details, see Set C++ compiler and build properties in Visual Studio.

  2. Select the Configuration Properties > C/C++ > Code Generation property page.

  3. Modify one or both of the following properties: Basic Runtime Checks or Smaller Type Check.

To set this compiler option programmatically

See also

MSVC compiler options
MSVC compiler command-line syntax
How to: Use native run-time checks