老宗师 2015-05-23 04:25 采纳率: 0%
浏览 2828

stack around variable was corrupted C++

每次运行结束后都会出现stack around variable “temp” was corrupted,
调试后发现总是在main程序结束后出现这个错误,这是为什么??

#include<iostream>
#include<string>
#include<fstream>
#include<assert.h>
#include<Windows.h>
using namespace std;

int UnicodeToANSI(char * pDes, const wchar_t * pSrc)
{
    assert(pDes != NULL);
    assert(pSrc != NULL);
    int nLen = ::WideCharToMultiByte(CP_ACP, 0, pSrc, -1, NULL, 0, NULL, NULL);
    if (nLen == 0)
    {
        return -1;
    }
    return ::WideCharToMultiByte(CP_ACP, 0, pSrc, -1, pDes, nLen, NULL, NULL);
}
int main()
{
    const int maxbytes = 50;
    wchar_t wbuf[2];
    memset(wbuf, 0, sizeof(wchar_t)*2);
    FILE *fp = NULL;
    FILE *outf = NULL;
    fopen_s(&fp,"D:\\Program Files\\新东方背单词5\\3K.txt","rb");  //unicode text file
    fopen_s(&outf, "D:\\Program Files\\新东方背单词5\\test.txt", "w");  //ansi text file
    if (fp != NULL)//&&outf!=NULL)
    {
        fseek(fp, sizeof(wchar_t), 0);  
        while (1)
        {
            char temp[2] = { 0 };
            fread(wbuf, sizeof(wchar_t), 1, fp);
            UnicodeToANSI(temp, wbuf);
            wchar_t widec = '!';
            if (wbuf[0] == widec)
            {
                break;
            }
            //cout << temp;
            fputs(temp, outf);
            memset(wbuf, 0, sizeof(wchar_t) * 2);           
        }
    }
    memset(wbuf, 0, sizeof(wchar_t) * 2);
    fclose(fp);
    fclose(outf);
    return 0;
} 
  • 写回答

2条回答 默认 最新

  • lvley1234 2015-05-23 04:54
    关注

    temp数组长度太短,导致数组越界了;
    int nLen = ::WideCharToMultiByte(CP_ACP, 0, pSrc, -1, NULL, 0, NULL, NULL); 返回值为3

    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?