问题

在用VS2008写一段代码,算法都没有问题,但是调试的时候发现出了main之后就报 Stack around the variable 'xxx' was corrupted 的错误,后来发现是数组越界造成的。测试下面类似情形的代码:

  1. #include <iostream>  
  2. using namespace std;  
  3. int main()  
  4. {  
  5.     int i, j, tmp;   
  6.     int a[10] = {0};// 0, 1, ... , 9   
  7.     for(i = 0; i < 10; ++i)  
  8.     {  
  9.         a[i] = 9 - i;  
  10.     }  
  11.     for(j=0; j<=9; j++)   
  12.     {   
  13.         for (i=0; i<10-j; i++)  
  14.         {  
  15.             if (a[i] > a[i+1])// 当j == 0时,i会取到,导致a[i+1]越界  
  16.             {   
  17.                 tmp = a[i];   
  18.                 a[i] = a[i+1];   
  19.                 a[i+1] = tmp;  
  20.             }   
  21.         }  
  22.     }   
  23.     for(i=1;i<11;i++)   
  24.     {  
  25.         cout << a[i]<<endl;  
  26.     }  
  27.     system("pause");  
  28.     return 0;  
  29. }   

出现下图 Debug Error:
Run-Time Check Failure #2 - Stack around the variable 'a' was corrupted

 
 

原因

Stack pointer corruption is caused writing outside the allocated buffer in stack memory.

解决方法

This kind of error is detected by setting /RTC1 compiler option from menu 属性页(Alt+F7) -> 配置属性 -> C++ -> 代码生成 -> 基本运行时检查
有以下几个选项:
(1) 默认值
(2) 堆栈帧 ( /RTCs )
(3) 未初始化的变量 ( /RTCsu )
(4) 两者 ( /RTC1, 等同与 /RTCsu )
(5) <从父级或项目默认设置继承>

方法1 :修改数组越界的错误。
方法2 :设置为 (1) 默认值,就不再进行 stack frame run-time error checking。

Using /RTC1 compiler option enables stack frame run-time error checking. For example, the following code may cause the above error messge.

  1. #include <stdio.h>  
  2. #include <string.h>  
  3. #define BUFF_LEN 11 // 12 may fix the Run-Time Check Failure #2  
  4. int rtc_option_test(char * pStr);  
  5. int main()  
  6. {  
  7.     char * myStr = "hello world";  
  8.     rtc_option_test(myStr);  
  9.     return 0;  
  10. }  
  11. int rtc_option_test(char * pStr)  
  12. {  
  13.     char buff[BUFF_LEN];  
  14.     strcpy(buff, pStr); //cause Run-Time Check Failure #2 - Stack around  
  15.     //the variable 'buff' was corrupted.  
  16.     return 0;  
  17. }  

 
 

参考
Stack around the variable was corrupted 解决方案
http://laokaddk.blog.51cto.com/368606/238718

stack around the variable was corrupted
http://www.cnblogs.com/hxf829/archive/2009/11/28/1659749.html

VS2008中Run-Time Check Failure #2 - Stack around the variable 'xxx' was corrupted 错误解决方法的更多相关文章

  1. Run-Time Check Failure #2 Stack around the variable ‘xxx’ was corrupted

    在改别人代码时,运行报错: Run-Time Check Failure #2 Stack around the variable 'buffer' was corrupted 这表明你对某变量的赋值 ...

  2. c++. Run-Time Check Failure #2 - Stack around the variable 'cc' was corrupted.

    Run-Time Check Failure #2 - Stack around the variable 'cc' was corrupted. char cc[1024];   //此处如果索引值 ...

  3. Run-Time Check Failure #2 - Stack around the variable 's' was corrupted. 出现了 。

    程序中存在内存越界,注意数组大小和数据大小.

  4. vs中 Stack around the variable 'XXX' was corrupted.

    https://blog.csdn.net/hou09tian/article/details/75042206 把 project->配置属性->c/c++->代码生成->基 ...

  5. Run-Time Check Failure #2 - Stack around the variable 'ucPriKey' was corrupt

    Run-Time    Check    Failure    #2        一般是栈被破坏,你的代码可能有缓冲区溢出一类的问题. Run-Time Check Failure #2 - Sta ...

  6. Stack around the variable 'szStr' was corrupted.

    错误:stack around the variable “XX” was corrupted.,中文翻译就是“在变量XX周围的堆栈已损坏”. 把 project->配置属性->c/c++ ...

  7. macOS 中使用 phpize 动态添加 PHP 扩展的错误解决方法

    使用 phpize 动态添加 PHP 扩展是开发中经常需要做的事情,但是在 macOS 中,首次使用该功能必然会碰到一些错误,本文列出了这些错误的解决方法. 问题一: 执行 phpize 报错如下: ...

  8. WPF:指定的命名连接在配置中找不到、非计划用于 EntityClient 提供程序或者无效的解决方法

    文/嶽永鹏 WPF 数据绑定中绑定到ENTITY,如果把数据文件做成一个类库,在UI文件中去应用它,可能遇到下面这种情况. 指定的命名连接在配置中找不到.非计划用于 EntityClient 提供程序 ...

  9. zend studio中ctrl+鼠标左键无法转到类或函数定义文件的解决方法

    转载自:http://blog.csdn.net/wide288/article/details/21622183 zend studio中ctrl+鼠标左键无法转到类或函数定义文件的解决方法: ze ...

随机推荐

  1. setcookie 设置无效

    转载请署名 achieverain,谢谢 经常见人问PHP程序无法设置cookie.下面把我遇见过的情况都说一遍 1 PHP程序在执行setcookie之前有输出. 解决 :    把setcooki ...

  2. C#学习心得,记录学习

  3. 关于PC端与手机端随着手指移动图片位置放生变化的拖拽事件

    当按下鼠标时,图片随鼠标移动松开时图片回到原位 drag("div_id") function drag(node_id){ var node = document.getElem ...

  4. Weex-进阶笔记二

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Helvetica; color: #945200 } p.p2 { margin: 0.0p ...

  5. NS_ASSUME_NONNULL_BEGIN 延伸

    NS_ASSUME_NONNULL_BEGIN和NS_ASSUME_NONNULL_END 在.h文件中,可以看到这两个宏,翻看定义,这两个宏的代码是 #define NS_ASSUME_NONNUL ...

  6. linux强制svn提交时必须写注释

    打开hooks,然后将pre-commit.tmpl修改为pre-commit,打开pre-commit,写入如下代码: #!/bin/sh REPOS="$1" TXN=&quo ...

  7. ServiceContract,OperationContract

    代码如下 [ServiceContract] //服务协定定义 using System.ServiceModel; public interface IInterface1 { [Operation ...

  8. UITextField 设置 placeholder 的字体颜色方法

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Helvetica; color: #ff2608 } [_textField setValu ...

  9. 魅族手机Listview下拉出现hold字样的奇葩问题解决方案

    前几天测试在魅族手机上测试,下拉listview,出现奇葩的hold字样,╭∩╮(︶︿︶)╭∩╮,这锅老子不背,但是问题还得解决吧,如下是对于魅族不同版本的出现这种情况的处理: 2.3之前: andr ...

  10. 《Windows编程循序渐进》——进度条

    界面布局如下: