首页 > 解决方案 > “.exe”已触发断点

问题描述

我继承了在 32 位中完美运行的代码,但是当我将编译器更改为 64 位时,如果我运行该代码,则会出现错误。如果我单步执行代码,它(几乎总是)会在以后失败。代码中有很多指针,C++ 不是我的强项。是否有人可以了解错误发生的原因?

我需要了解的是:是否可以先创建 BYTE***,然后再创建 BYTE**,并且永远不会分配 BYTE*。我不明白这是如何工作的。我认为首先分配 BYTE*,然后是 BYTE**,然后是 BYTE***。为什么这适用于 x86 而不是 x64?

void* AllocMemory(SIZE_T dwBYTEs)
{
    void* result;
    if (dwBYTEs > 0)
    {
        result = malloc(dwBYTEs);
        if (result != NULL)
            return result;
    }
    return 0;
}

BYTE** CreateBytes(int arg_0, int arg_4, int arg_8)
{
    BYTE** hedi = (BYTE**)AllocMemory(arg_0 * 4);
    if (!hedi)return 0;
    hedi[0] = (BYTE*)AllocMemory(arg_0 * arg_8 * arg_4);
    if (!hedi[0]) {
        delete(hedi);
        return 0;
    }
    for (int i = arg_0 - 1; i > 0; i--) {
        hedi[arg_0 - 1 - i + 1] = &hedi[0][arg_4 * arg_8 * (arg_0 - 1 - i + 1)];
    }
    return hedi;
}

BYTE*** CreateItem(int arg_0, int arg_4, int arg_8)
{
    BYTE*** hebp = (BYTE***)AllocMemory(arg_8 * 4);
    hebp[0] = (BYTE**)CreateBytes(arg_0, arg_4 * arg_8, 1);
    if (!hebp[0])
    {
        delete(hebp);
        return 0;
    }
    for (int i = 1; i < arg_8; i++) {
        hebp[i] = (BYTE**)AllocMemory(arg_0 * 4);
        if (hebp[i]) {
            for (int j = 0; j < arg_0; j++) {//eax
                hebp[i][j] = &hebp[0][j][i];
            }
        }
        else {
            for (int j = i - 1; j > 0; j--) {//esi
                delete(hebp[j - i]);
            }
            delete(hebp[0]);
            delete(hebp);
            return 0;
        }
    }
    return hebp;
}

标签: c++memory-managementmalloc

解决方案


这是你的问题:AllocMemory(arg_0 * 4)

在 32 位系统上,指针的大小是 32 位,即四个字节。在 64 位系统上,指针是 64 位,8个字节。使用正确的尺寸(使用sizeof运算符),它应该可以正常工作。


例如,在CreateBytes您拥有的功能中

BYTE** hedi = (BYTE**)AllocMemory(arg_0 * 4);

将其替换为

BYTE** hedi = (BYTE**)AllocMemory(arg_0 * sizeof *hedi);

还有许多其他问题,例如代码分配内存malloc和释放内存delete。这会导致未定义的行为

而且我什至不会提及变量的命名或缺少注释...


推荐阅读