如何以指向 0xCCCCCCCC 的指针结束

2022-01-05 00:00:00 visual-studio-2008 c++

我正在处理的程序有时会在尝试读取地址 0xCCCCCCCC 处的数据时崩溃.Google(和 StackOverflow)是我的朋友,我看到它是未初始化堆栈变量的 MSVC 调试代码.为了了解问题的来源,我尝试重现此行为:问题是我无法做到.

The program I'm working on crashes sometimes trying to read data at the address 0xCCCCCCCC. Google (and StackOverflow) being my friends I saw that it's the MSVC debug code for uninitialized stack variable. To understand where the problem can come from, I tried to reproduce this behavior: problem is I haven't been able to do it.

问题是:您是否有代码片段显示指针如何结束指向0xCCCCCCCC?

Question is: have you a code snippet showing how a pointer can end pointing to 0xCCCCCCCC?

谢谢.

推荐答案

使用 /GZ 编译器开关 或 /RTCs 开关.确保 /Od 开关也用于禁用任何优化.

Compile your code with the /GZ compiler switch or /RTCs switch. Make sure that /Od switch is also used to disable any optimizations.

s

启用堆栈帧运行时错误检查,如下所示:

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

  • 局部变量初始化为非零值.这有助于识别在调试模式下运行时未出现的错误.由于发布版本中堆栈变量的编译器优化,与发布版本相比,调试版本中堆栈变量仍然为零的可能性更大.一旦程序使用了其堆栈区域,编译器永远不会将其重置为 0.因此,碰巧使用相同堆栈区域的后续未初始化堆栈变量可以返回先前使用该堆栈内存时留下的值.

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

检测局部变量(例如数组)的溢出和不足./RTCs 在访问由结构中的编译器填充导致的内存时不会检测溢出.通过使用 align (C++)、/Zp(结构成员对齐)或 pack,或者如果您以要求编译器添加填充的方式对结构元素进行排序,则可能会发生填充.

Detection of overruns and underruns of local variables such as arrays. /RTCs 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.

堆栈指针验证,检测堆栈指针损坏.调用约定不匹配可能会导致堆栈指针损坏.例如,使用函数指针调用 DLL 中的函数,该函数导出为 __stdcall,但将指向该函数的指针声明为 __cdecl.

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.

相关文章