Run-Time Check Failure #2分析(转载)

 

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
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值