Run-Time Check Failure #2分析

来源:互联网 发布:淘宝刷购物车 编辑:程序博客网 时间:2024/05/29 06:35

Run-Time   Check   Failure   #2   
  一般是栈被破坏,代码可能有缓冲区溢出一类的问题。有的是调用代码中的临时变量在函数调用结束时提示出错。

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

 http://www.codeguru.com/forum/showthread.php?t=299770

http://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/14e7318e-6fff-4d68-a823-9cbe7b7bc20a/

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一类的函数,造成目标区与初始定义的大小不一致了。

如:

void 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';

}

有的情况对运行的结果不会有影响,只要计算结果的情形下可暂忽略这个问题。

0 0