代码之家  ›  专栏  ›  技术社区  ›  Jaques

“.exe”已触发断点

  •  0
  • Jaques  · 技术社区  · 6 年前

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

    我需要理解的是:先创建字节***,然后再创建字节**,可以吗, 和 字节*从未分配给。我不明白这是怎么回事。我认为字节*首先被分配,然后是字节**,然后是字节***。为什么这适用于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;
    }
    
    1 回复  |  直到 6 年前
        1
  •  5
  •   Some programmer dude    6 年前

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

    在32位系统上,指针的大小为32位,即四个字节。在64位系统上,指针是64位, 字节。使用正确的尺寸(使用 sizeof 操作员)并且应该工作正常。


    例如,在 CreateBytes 您拥有的功能

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

    换成

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

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

    我甚至不会提到变量的命名或缺少注释。。。