runtime_checks

/RTC (Run-Time Error Checks)


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

/RTC1
/RTCc
/RTCs
/RTCu

1

Equivalent of /RTCsu.

c

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

This option reports situations in which you intend to truncate, for example, if you want the first eight bits of an int returned as a char. Because /RTCc causes a run-time error if any information is 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);
}
s

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

  • Initialization of local variables to a nonzero value. This helps identify bugs that do not appear when running in debug mode. There is a greater chance that stack variables will still be zero in a debug build compared to a release build because of compiler optimizations of stack variables in a release build. Once a program has used an area of its stack, it is never reset to 0 by the compiler. Therefore, subsequent, uninitialized stack variables that happen to use the same stack area can return values left over from the prior use of this stack memory.

  • Detection of overruns and underruns of local variables such as arrays. /RTC s will not detect overruns when accessing memory that results from compiler padding within a structure.Padding could occur by using align (C++)/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.

u

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 generatesCompiler 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 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.

Run-time error checks are a way for you to find problems in your running code; for more information, seeHow to: Use Native Run-Time Checks.

If you compile your program at the command line using any of the /RTC compiler options, any pragmaoptimize instructions in your code will silently fail. This is because run-time error checks are not valid in a release (optimized) build.

You should use /RTC for development builds; /RTC should not be used for a retail build. /RTC cannot be used with compiler optimizations (/O Options (Optimize Code)). A program image built with /RTC will be 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 How to: Open Project Property Pages.

  2. Click the C/C++ folder.

  3. Click the Code Generation property page.

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

To set this compiler option programmatically

runtime_checks


Disables or restores the /RTC settings.

#pragma runtime_checks( "[runtime_checks]", {restore | off} )

You cannot enable a run-time check that was not enabled with a compiler option. For example, if you do not specify /RTCs, specifying #pragma runtime_checks( "s", restore) will not enable stack frame verification.

The runtime_checks pragma must appear outside a function and takes effect at the first function defined after the pragma is seen. The restore and off arguments turn options specified in the runtime_checks on or off.

The runtime_checks can be zero or more of the parameters shown in the following table.

Parameters of the runtime_checks Pragma

Parameter(s)

Type of run-time check

s

Enables stack (frame) verification.

c

Reports when a value is assigned to a smaller data type that results in a data loss.

u

Reports when a variable is used before it is defined.

These are the same letters used with the /RTC compiler option. For example:

#pragma runtime_checks( "sc", restore )

Using the runtime_checks pragma with the empty string ("") is a special form of the directive:

  • When you use the off parameter, it turns the run-time error checks, listed in the table above, off.

  • When you use the restore parameter, it resets the run-time error checks to those that you specified with the /RTC compiler option.

#pragma runtime_checks( "", off )
.
.
.
#pragma runtime_checks( "", restore ) 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值