晚上花了几个小时fix了这个恼人的BUG!“在变量XX周围的堆栈已损坏”

在网上找到的解释是:

把“project->配置属性->c/c++->代码生成->基本运行时检查 设置为默认值,就没有这样的错误了。关于MSDN的解释是在堆栈外面读写某数据。错误是名为RTC1的编译器检测的。又看了更多的技术文章,发现这样的错误是程序员在项目到了一定大的时候,它占用的堆栈量就比较大。我也深有体会。因为自己本来编写一个类,运行时没有错,但是在添加成员属性的时候,在其它方式不变的情况下就容易发生这样的错误。所以据此我猜应该是VS在内部就限定了堆栈的大小,当项目足够大的时候,就会溢出。

 char *transformation(char *str)  //在字符串插入字符的个数,如"aaabb"  转换后为 aaa3bb2
{
if (NULL == str)
{
return NULL;
}
char *temp =str;
int len =strlen(str);
int count = ;
char* buf = new char[len+];
char* pos =str;
char* pos1 = str+;
while (*str)
{
if (*pos++ == *pos1++)
{
count++;
}
else
{
int2str(count,buf); //本来开始准备用_itoa_s发现不好用,自己写了一个整形转字符串函数
int nbit = strlen(buf);
strcat_s(buf,len+,pos);
*pos=;
strcat_s(str,*len,buf);
pos +=nbit;
pos1+=nbit;
str = pos;
count =;
}
}
delete []buf;
return temp; }

我在函数栈内申请了一个buf的字符数组,最开始我设置数组长度为200出现了BUG,设置为len+1后没报BUG,可能就是因为堆栈占用过多。

还有想吐槽一下 C++里的 strcat_s(),_itoa_s()函数用起来很不爽,还要指定长度,虽然这样可以防止溢出,但是我觉得用起来不爽,为什么_strcat_s()不在函数里面调用strlen()检查,还要程序员自己指定,不如C里面的strcat()好用

stack around the variable “XX” was corrupted的更多相关文章

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

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

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

    问题 : 在用VS2008写一段代码,算法都没有问题,但是调试的时候发现出了main之后就报 Stack around the variable 'xxx' was corrupted 的错误,后来发 ...

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

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

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

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

  5. 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];   //此处如果索引值 ...

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

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

  7. vs中“Stack around the variable was corrupted”的解决方案

    把 project->配置属性->c/c++->代码生成->基本运行时检查 为 默认值 就不会报本异常.具体原因正在研究中... 如果改为其他就有exception. exce ...

  8. 运行时错误:“stack around the variable…was corrupted”

    造冰箱的大熊猫@cnblogs 2018/11/1 引发问题的代码片段如下 WORD var; scanf ( "%d", &var ); 包含上述代码的程序,编译正常,运 ...

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

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

随机推荐

  1. Spring笔记(四)SpingAOP

    需要的Jar包(String3.2) com.springsource.net.sf.cglib-2.2.0.jar // 作用于cglib方式的动态代理 com.springsource.org.a ...

  2. Could not load db driver class: com.mysql.jdbc.Driver解决方法

    14/03/26 22:43:24 ERROR sqoop.Sqoop: Got exception running Sqoop: java.lang.RuntimeException: Could ...

  3. HDOJ-ACM1017(JAVA)

    问题描述: 简单来说,就是 输入N,然后输入N个数据块,一个数据块包含(n,m),并计算0 < a < b < n 且 (a^2+b^2 +m)/(ab) 为整数.其中a,b只要符合 ...

  4. Dynamices CRM JS 类库 神器 XrmServiceToolkit - A Microsoft Dynamics CRM 2011 & CRM 2013 JavaScript Library

    XrmServiceToolkit - A Microsoft Dynamics CRM 2011 & CRM 2013 JavaScript Library http://xrmservic ...

  5. csipsimple 下载地址

    项目下载地址: http://git.oschina.net/zencodex/CSipSimple https://github.com/zencodex/csip

  6. tcpclient 类

    1. 构造函数 1) 类对象将套接字与本地系统地址和一个随机的tcp端口号进行绑定. 在默认的tcpclient 对象创建后,必须使用connect方法与远程设备连接. TcpClient tc = ...

  7. key_t键和ftok函数

    系统建立IPC通讯(如消息队列.共享内存时)必须指定一个ID值.通常情况下,该id值通过ftok函数得到. ftok原型如下: key_t ftok( char * fname, int id ) f ...

  8. 巧用TAG属性保存对象的指针

    指针的数据类型是整型,一个指针就是一个整型的数值. 所以凡整型的变量(这个整型的变量可以是声明在INI文件中,也可以是声明在函数中的)也好,对象的整型的属性也好,都可以用来存储一个指针. 但对象往往没 ...

  9. oc学习之路----application.keyWindow.rootViewController与self.window.rootViewController与[self.window makeKeyAndVisible];小发现

    刚才调试的时候除了一些小问题,有一段代码要重用 NSString *key = @"CFBundleVersion"; // 取出沙盒中存储的上次使用软件的版本号 NSUserDe ...

  10. iOS ipv6

    这当中最重要的两个概念是DNS64和NAT64. DNS64 DNS64说白了是用来帮助host获取IPv6地址的,传统的DNS服务器可以把域名转换成IPv4地址,但我们的iPhone设备如果处于IP ...