zoukankan      html  css  js  c++  java
  • 为什么提示此错误?RunTime 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;

  • 相关阅读:
    GUI 监听事件 (两个按钮,实现同一个监听)
    GUI 监听事件
    GUI 练习
    GUI 之表格布局
    GUI 之边界布局
    GUI 之流布局
    [转帖]Linux 下解压 rar 文件
    Linux 启动、停止、重启jar包脚本
    关于linux下,ls vi等命令失效的解决方法(配置下环境变量出现问题)
    超好用的UnixLinux 命令技巧 大神为你详细解读
  • 原文地址:https://www.cnblogs.com/carl2380/p/2086011.html
Copyright © 2011-2022 走看看