[转]Run-Time Check Failure #2分析

原文:http://blog.sina.com.cn/s/blog_616ac4f00100ym8r.html

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

Run-Time Check Failure #2 - Stack around the variable 's' was corrupted

This problem is caused when you try to write too much data to a particular memory address. Typical causes are writing more to a string buffer than you have room for.
可能会有以下几种情况:
一、 strcpy、memcpy、strncpy、stpcpy、bcopy等拷贝区的大小不匹配,引起冲突或溢出造成的。
例如:
void myfun()
{
char mybuf[10]; 
strcpy(mybuf, "This is definitely more than 10 characters long, it will also cause a Run-Time Check");
}
二、当使用 memset/ZeroMemory初始化数据结构体 structure或数组 array时,由于大小设置错误引起的。例如:struct MyStruct
{
int var;
};
void myfun2()
{
MyStruct ms;
ZeroMemory(&ms, 20); //since MyStruct is only one variable in the struct this will cause problems
}
三、可能是指针移动,指向错误,例如: 
void myfun3()
{
int a;
int*b = &a; 
a++;
*a = 20;
}
四、可能是使用了itoa一类的函数,造成目标区与初始定义的大小不一致了。
如:
oid myfun4()
{
int b=54698369;
char ch1[8];
itoa(b,ch,16);
}
总之,出现这类错要仔细检查提示错误的变量variable "s",将其大小修改为匹配。
如四中:
Run-Time Check Failure #2 - Stack around the variable 'ch1' was corrupted,提示错误的变量variable "ch1",将它增加一个空间作为终止记号的存放处:
void myfun4()
{
int b=54698369;
char ch1[9];
itoa(b,ch,16);
ch1[8]='\0';
}
五:还有可能是变量赋值了,但是变量没有找到

-------------------------------------

问题看似不严重,但是都是隐患,需要仔细排查debug



猜你喜欢

转载自blog.csdn.net/xian_wwq/article/details/72519080