简体   繁体   English

如何修复C ++中的“堆已损坏”错误?

[英]How to fix 'Heap has been corrupted 'error in c++?

When I run the program, I get exception "heap has been corrupted" after completion of the function 当我运行该程序时,在完成该功能后会出现异常“堆已损坏”

I have read that this exception may cause if you are using memory that has been freed, or when you are writing to index which is out of array index. 我已经读到,如果您使用的是已释放的内存,或者正在写入数组索引之外的索引,则可能导致此异常。 But none of the cases applies here. 但是,这里没有任何一种情况适用。 I have read other answers of some problems but it didn't help much. 我已经阅读了一些问题的其他答案,但并没有太大帮助。

`char fileNametoExport[26]="d:\\FOlder1\\part1.ipt";
 char WorkingFolderName[260] ="d:\\folder";
 int start = rFind(fileNametoExport, '\\');
 int finish = rFind(fileNametoExport, '.');
 if (start == -1)
 start = 0;
char partname[260];
strcpy(partname,substr(fileNametoExport, start, finish));
::AfxMessageBox((LPCTSTR)partname);
char xtfile[260];
char xmltxtfile[260];
strcpy(xtfile, strcat(WorkingFolderName, partname));
strcat(xtfile, "__Default.x_t");
strcpy(xmltxtfile, WorkingFolderName);
strcat(xmltxtfile,"_XT_SE_INV_Default_SOLID_0_Solid1_xt.xmt_txt");`

function rfind() to find occurence of char in char array- 函数rfind()查找char数组中char的出现-

int rFind(char* s, char c)
    {
    int sz = 0;
    char *tmp = s;
    while (*tmp != '\0')
    {
        sz++;
        tmp++;
    }
    for (int i = sz - 1; i >= 0; i--)
    {
        if (*(s + i) == c)
            return i;
    }
    return -1;
}

function substr() to get substring from position x to y (y exclusive) 函数substr()从位置x到y获取子字符串(y排他)

char* substr(char* s, const int b, const int f)
{
    char *str = new char[f - b];
    int t = 0;
    for (int i = b; i != f; i++)
    {
        str[t] = s[i];
        t++;
    }
    str[t] = '\0';
    return str;
}

PS- While giving input I ensure that fileNametoExport always contains '.' PS-在输入时,我确保fileNametoExport始终包含“。”。 and '\\'. 和'\\'。

  1. Your program do not check lengths of input strings. 您的程序不检查输入字符串的长度。 You can receive a string longer than your buffer and program will fail. 您收到的字符串长于缓冲区,程序将失败。
  2. If your program get fileNametoExport = "d:\\\\somefolder\\\\somefilewithoutdot" , finish will be -1 and program fail at strcpy(partname,substr(fileNametoExport, start, finish)); 如果您的程序获得fileNametoExport = "d:\\\\somefolder\\\\somefilewithoutdot" ,则finish将为-1,并且程序在strcpy(partname,substr(fileNametoExport, start, finish));处失败strcpy(partname,substr(fileNametoExport, start, finish)); .
  3. Program writes after buffer in char* substr(char* s, const int b, const int f) at line 程序在char* substr(char* s, const int b, const int f)中的缓冲区之后写入

     str[t] = '\\0'; 

    because t at this point equal fb , size of str buffer. 因为此时t等于fb ,所以str缓冲区的大小。

Function _ASSERTE( _CrtCheckMemory( ) ); 函数_ASSERTE( _CrtCheckMemory( ) ); from <crtdbg.h> very useful when searching for bugs like this. 搜索此类错误时,来自<crtdbg.h>非常有用。 Put it around suspicious code and it fails after your bug. 将其放在可疑代码周围,并在发生错误后失败。 It works only in debug. 它仅在调试中有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM