在Y上修改的堆塊的大小為2個錯誤。

[英]Heap block at X modified at Y past requested size of 2 ERROR


So I am getting an error at the end of the following code that I completely do not understand:

所以我在下面的代碼末尾得到了一個我完全不理解的錯誤:

    char* registerPointer = NULL;
    // parse the currentLine
    char** rt_rs_immediate;
    rt_rs_immediate = malloc(3 * sizeof(char*));
    if (rt_rs_immediate == NULL ) {
        fprintf(outputFilePointer, "no more memory");
        exit(1);
    }

    for (int i = 0; i < 3; i++) {
        rt_rs_immediate[i] = malloc(2 * sizeof(char));
        if (rt_rs_immediate[i] == NULL ) {
            fprintf(outputFilePointer, "no more memory");
            exit(1);
        }
    }

    int indexWithin_rt_rs_immediate = 0;
    registerPointer = strtok(currentLine, " $,\n\t");
    while (registerPointer != NULL ) {
        if (registerPointer == NULL || *registerPointer == '#') {
            break;
        } else {
            strcpy(rt_rs_immediate[indexWithin_rt_rs_immediate],
                    registerPointer);
            indexWithin_rt_rs_immediate++;
            registerPointer = strtok(NULL, " $,\n\t");
        }
    }
    free(registerPointer);

    // write to outputFile
    int immediate = atoi(rt_rs_immediate[2]);
    writeOutI_TypeInstruction(I_TypeInstruction, rt_rs_immediate[1],
            rt_rs_immediate[0], immediate, outputFilePointer);
    // free pointers created with malloc
    for (int i = 0; i < 3; i++) {
        free(rt_rs_immediate[i]); //<====================ERROR HERE!!!!!
    }
    free(rt_rs_immediate);

1 个解决方案

#1


1  

Your while loop terminates when regsiterPointer is a NULL. You may want to check on that loop because you only have three pointers allocated to rt_rs_immediate. If you try to go past the three pointers in that loop, that could cause an error.

當regsiterPointer為空時,while循環終止。您可能希望檢查該循環,因為只有三個指針分配給rt_rs_immediate。如果您試圖通過該循環中的三個指針,可能會導致錯誤。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2013/11/25/720f6b3479cff9b48ac442f961191f89.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com