• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
张纯睿
博客园    首页    新随笔    联系   管理    订阅  订阅

为什么提示此错误?Run-Time Check Failure #2 - Stack around the variable 'tch1'was corrupted.

参考: http://topic.csdn.net/u/20081006/16/859ded6d-7ef1-4b72-b9d8-5ce5d2d7da89.html

CString CMacroCmd::Ascii2String(CString strAscii)

{

#ifdef UNICODE

         CString strValue = _T("");

         TCHAR tch1 = 0x0000;

         for (int i=0; i<strAscii.GetLength()/4; i++)

         {

                   CString stemp1 = strAscii.Mid(i*4, 4);

 

                   _stscanf_s(stemp1.GetBuffer(0),_T("%x"),&tch1);!!!

 

                   CString stemp3;

                   stemp3.Format(_T("%c"), tch1);

                   strValue = strValue+stemp3;

         }

         return strValue;

#else

         ASSERT(FALSE);    //       必À?须?是º?UNICODE

         return strAscii;

#endif      

}

程序运行完 _stscanf_s(stemp1.GetBuffer(0),_T("%x"),&tch1);

后在离开次函数时弹出错误提示:
Run-Time Check Failure #2 - Stack around the variable 'tch1'was corrupted.
请教高人这是怎么回事?

-----参考解答:

sscanf 是一个非常不安全的函数,定义 WORD Step 则 sizeof(Step) = 2,
VS2005 下 _stscanf_s( XX, _T("%d"), &Step ) 会将输出参数当作 int 类型来写入, sizeof(int) 是4个字节。
这样会导致多写入2个字节,造成数据的意外破坏。

而VC6没问题应该是VC6将这个优化了或者忽略了。

解决方法:int tch1 = 0; //TCHAR tch1 = 0x0000;

posted @ 2011-06-21 14:21  张纯睿  阅读(280)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3