菜单 学习猿地 - LMONKEY

VIP

开通学习猿地VIP

尊享10项VIP特权 持续新增

知识通关挑战

打卡带练!告别无效练习

接私单赚外块

VIP优先接,累计金额超百万

学习猿地私房课免费学

大厂实战课仅对VIP开放

你的一对一导师

每月可免费咨询大牛30次

领取更多软件工程师实用特权

入驻
411
0

VS2008中Run-Time Check Failure #2 - Stack around the variable 'xxx' was corrupted 错误解决方法

原创
05/13 14:22
阅读数 41601

问题

在用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

发表评论

0/200
411 点赞
0 评论
收藏