转载:Run-Time Check Failure #2分析

文章出处:http://hi.baidu.com/%D6%BB%D3%B0%C1%F7%C0%CB/blog/item/0d72bcb477b2bbc236d3cad4.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';
}

你可能感兴趣的:(转载:Run-Time Check Failure #2分析)