问题

在用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. 制作jar包

    1.打开cmd 2.通过cd切换到要打包的工程所在的bin目录(一定是bin目录) 运行jar -cvf aa.jar *.* jar是打包的命令 -cvf可以自行查看一下文档解释(jar -help ...

  2. 【翻译】创建Cordova项目

    下载或者更新Node.js到最新版本 在电脑终端输入命令来安装Cordova CLI sudo npm install -g cordova 如果使用Windows电脑,需要在使用cmd命令行输入 n ...

  3. schema change + ogg 变更手册

    Check OGG  until no data queuing in replication process:testRO:a)login  test5 –l oggmgrb)oggc)#ggsci ...

  4. freemarker(FTL)常见语法大全

    [转载]freemarker(FTL)常见语法大全 FreeMarker的插值有如下两种类型:1,通用插值${expr};2,数字格式化插值:#{expr}或#{expr;format}  ${boo ...

  5. intellij idea 12 搭建maven web项目 freemarker + spring mvc

    配置spring mvc ,写这篇文章的时候spring已经出了4.0 这里还是用稳定的3.2.7.RELEASE,先把spring和freemarker配置好 1.spring mvc配置 在web ...

  6. OGG强制停止进程

    1.异常现象[oracle@localhost ~]$ ggsci Oracle GoldenGate Command Interpreter for OracleVersion 11.1.1.0.0 ...

  7. DHCP底层参考

    [原创翻译,水平有限] ISC DHCP支持802.1的以太网帧,令牌环和FDDI等网络.为了桥接物理层和DHCP层,它还必须实现IP和UDP协议帧. 这源于UNIX BSD socket 对未配置接 ...

  8. 淘淘商城_day09_课堂笔记

    今日大纲 实现购物车 基于Mysql实现读写分离 购物车 需求描述 用户可以在登录状态下将商品添加到购物车 用户可以在未登录状态下将商品添加到购物车 用户可以使用购物车一起结算下单 用户可以查询自己的 ...

  9. Ubuntu下NFS,TFTP服务搭建

    环境:Ubuntu 一. 搭建NFS服务器 (1)安装: sudo apt-get install nfs-kernel-server   #安装NFS服务器端 sudo apt-get instal ...

  10. Date 和 SimpleDateFormat 类表示时间

    Date now=new Date(); // 使用format()方法将日期转换为指定格式的文本 SimpleDateFormat sdf1 = new SimpleDateFormat(" ...