问题

在用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. bison实例

    逆波兰记号计算器[文件名rpcalc.y]%{ #define YYSTYPE double #include <stdio.h> #include <math.h> #inc ...

  2. Thrift框架简介

    功能:实现各个服务模块之间的跨语言.跨平台的通信,是RPC框架的一种,与dubbo类似. Thrift的应用原理: Thrift的部分功能相当于代码生成引擎,使用Thrift定义的语言编写*.Thri ...

  3. yii2.0 面包屑的使用

    yii2中面包屑是yii2自带的小部件,类似本网站的导航栏应该就是采用面包屑来完成的 例子如下,需要引入 yii\widgets\Breadcrumbs echo Breadcrumbs::widge ...

  4. css的三大特性

    1.继承性 作用:子元素可以继承父元素的样式text-,font-,line-这些元素开头的都可以继承,以及color属性特殊性: ①. a标签的颜色不能继承,必须对a标签本身进行设置 ②. h标签的 ...

  5. Python基础篇-day6

    本节简介: 1.模块1.1 时间模块1.2 random模块1.3 shutil模块1.4 shelve模块1.5 XML模块1.6 ConfigParser模块1.7 hashlib模块1.8 lo ...

  6. CocoaPods 报错 [!] Error installing JSONModel

    pod install p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #34bd26 } span.s1 { } ...

  7. Intent Flag实际项目 -- 超时跳转登录界面并清理前面所有activity

    项目中涉及到登录超时跳转登录界面的逻辑,我以前的跳转flag为Intent.FLAG_ACTIVITY_CLEAR_TOP,但是点击返回按钮还是会回到上个界面.代码如下: ActivityUtils. ...

  8. 《JS权威指南学习总结--9.3 JS中JAVA式的类继承》

    内容要点: 一.JS中的类 1.JAVA或其他类似强类型 面向对象语言的 类成员的模样 实例字段:它们是基于实例的属性或变量,用以保存独立对象的状态. 实例方法: 它们是类的所有实例所共享的方法,由每 ...

  9. Django中的权限系统

    Django中已经为我们设置好了基本的权限系统,在定义好model同步数据库后,在每个库下面都会有一张 'auth_permission' 表.该表里面记录了每条权限的描述(name字段,can do ...

  10. linux命令readlink

    readlink,从字面意思就可以看出来,输出符号链接值或者权威文件名. openwrt 下的readlink命令参数如下: root@hbg:/# readlink  --helpBusyBox v ...