c++ 中的堆损坏

发布于 2024-10-19 01:27:35 字数 941 浏览 6 评论 0原文

当我在 Visual C++ 2010 中运行代码时,我在运行时遇到错误。

void dct2(){
    float** G = new float*[len];
    for(int u = 0; u < len; u++){
        G[u] = new float[len];
        for(int v = 0; v < len; v++){
            G[u][v] = 0;
            for(int x = 0; x < len; x++){
                for(int y = 0; y < len; y++){
                    G[u][v] += a(u) * a(v) * (float)mat[x][y] * cos((PI / 8) * u * (x + 0.5)) * cos((PI / 8) * v * (y + 0.5));
                }
            }
        }
    }
    doublecpy(G);
}
void doublecpy(float** d){
    for(int i = 0; i < len; i++){
        for(int j = 0; j < len; j++){
            if(d[i][j] >= 0)
                mat[i][j] = (int)(d[i][j] + 0.5);
            else
                mat[i][j] = (int)(d[i][j] - 0.5);
        }
    }
    for(int i = 0; i < len; i++)
        delete[] d[i];
    delete[] d;
}

错误出现在以下行中:delete[] d[i]; 请告诉我这段代码是否有任何问题或任何建议。

I'm getting an error in runtime when I run my code in visual c++ 2010.

void dct2(){
    float** G = new float*[len];
    for(int u = 0; u < len; u++){
        G[u] = new float[len];
        for(int v = 0; v < len; v++){
            G[u][v] = 0;
            for(int x = 0; x < len; x++){
                for(int y = 0; y < len; y++){
                    G[u][v] += a(u) * a(v) * (float)mat[x][y] * cos((PI / 8) * u * (x + 0.5)) * cos((PI / 8) * v * (y + 0.5));
                }
            }
        }
    }
    doublecpy(G);
}
void doublecpy(float** d){
    for(int i = 0; i < len; i++){
        for(int j = 0; j < len; j++){
            if(d[i][j] >= 0)
                mat[i][j] = (int)(d[i][j] + 0.5);
            else
                mat[i][j] = (int)(d[i][j] - 0.5);
        }
    }
    for(int i = 0; i < len; i++)
        delete[] d[i];
    delete[] d;
}

the error comes in the line: delete[] d[i];
please tell me if there's anything wrong with this piece of code or any advice.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

很酷不放纵 2024-10-26 01:27:35

除了你不应该以这种方式编写 C++ 的事实之外(这只是带有 new 而不是 malloc 的 C),我看不到任何内存错误,但不知道 mat 是什么以及它是如何分配的。

Apart from the fact you should not write C++ this way (this is just C with new instead of malloc) I can't see any memory errors but do not know what mat is and how it was allocated.

予囚 2024-10-26 01:27:35

我修改了您的离散余弦变换和蝴蝶代码以适合我的 C 编译器(不是 C++)。我为那些我没有的虚拟函数和常量插入了。它可以工作(我测试了 len=1000),并且不会损坏内存。我的版本工作的原因可能是 new float*[len],您可以将其重写为 malloc(len*sizeof(float*))尝试一下这是否是问题所在。

(我手头没有c++编译器,无法自己尝试)
顺便说一句,我发现有趣的是,您在 dct2() 中new,但在 doublecpy() 中delete

反正。这是我改编的代码:

    #include <stdio.h>
    #include <stdlib.h>

    #define len 1000
    #define PI 3.14

    float mat[len][len];

    float a(int u) {
            return 1.0;
    }

    void doublecpy(float** d){
            int i;
        for(i = 0; i < len; i++){
                    int j;
            for(j = 0; j < len; j++){
                if(d[i][j] >= 0)
                    mat[i][j] = (int)(d[i][j] + 0.5);
                else
                    mat[i][j] = (int)(d[i][j] - 0.5);
            }
        }
        for(i = 0; i < len; i++)
            free(d[i]);
        free(d);
    }


    void dct2(){
        float** G = malloc(len*sizeof(float*));//new float* [len];
        int u;
        for(u = 0; u < len; u++){
            G[u] = malloc(len*sizeof(float));
            int v;
            for(v = 0; v < len; v++){
                G[u][v] = 0;
                int x;
                for(x = 0; x < len; x++){
                                    int y;
                    for(y = 0; y < len; y++){
                        G[u][v] += a(u) * a(v) * (float)mat[x][y] * cos((PI / 8) * u * (x + 0.5)) * cos((PI / 8) * v * (y + 0.5));
                    }
                }
            }
        }
        doublecpy(G);
    }
    int main()
    {
            dct2();
            getch();
    }

I modified your discrete cosine transform and butterfly code to fit my C compiler (not C++). I inserted dummy-functions and constants for those that i dont have. It works (i tested it up to len=1000) without corrupting memory. The reason my version works may be new float*[len], which you could rewrite to malloc(len*sizeof(float*)) to try out if that was the problem.

(I dont have a c++-compiler handy to try it myself)
By the way, i find it intersting that you new in dct2() but delete in doublecpy().

Anyway. This is my adapted code:

    #include <stdio.h>
    #include <stdlib.h>

    #define len 1000
    #define PI 3.14

    float mat[len][len];

    float a(int u) {
            return 1.0;
    }

    void doublecpy(float** d){
            int i;
        for(i = 0; i < len; i++){
                    int j;
            for(j = 0; j < len; j++){
                if(d[i][j] >= 0)
                    mat[i][j] = (int)(d[i][j] + 0.5);
                else
                    mat[i][j] = (int)(d[i][j] - 0.5);
            }
        }
        for(i = 0; i < len; i++)
            free(d[i]);
        free(d);
    }


    void dct2(){
        float** G = malloc(len*sizeof(float*));//new float* [len];
        int u;
        for(u = 0; u < len; u++){
            G[u] = malloc(len*sizeof(float));
            int v;
            for(v = 0; v < len; v++){
                G[u][v] = 0;
                int x;
                for(x = 0; x < len; x++){
                                    int y;
                    for(y = 0; y < len; y++){
                        G[u][v] += a(u) * a(v) * (float)mat[x][y] * cos((PI / 8) * u * (x + 0.5)) * cos((PI / 8) * v * (y + 0.5));
                    }
                }
            }
        }
        doublecpy(G);
    }
    int main()
    {
            dct2();
            getch();
    }
待"谢繁草 2024-10-26 01:27:35

正如我在评论中所说,我看不出有什么问题......

尝试使用 duma 等工具来检查损坏情况。 ..my2c

As said in my comment, I can not see anything wrong ...

Try tools like duma to check for corruption ...

my2c

小镇女孩 2024-10-26 01:27:35

我开始将所有数组转换为 std::vector ,并在中途运行代码。这并没有解决我的问题,但我找到了错误的原因。就在这个函数中:

void cpy(int** arr, int x, int y){
    for(int i = x; i < x + len; i++){
        for(int j = y; j < y + len; j++){
            mat[i][j] = arr[i][j];
        }
    }
}

错误非常明显,我纠正了它:

void cpy(int** arr, int x, int y){
    for(int i = 0; i < len; i++){
        for(int j = 0; j < len; j++){
            mat[i][j] = arr[i + x][j + y];
        }
    }
}

虽然这很奇怪。我预计在运行以下行中的错误代码时会收到“数组索引越界”或“分段错误”: mat[i][j] = arr[i][j];

无论如何,感谢大家的帮助。

I started converting all arrays to std::vector and in the midway i ran the code. That didn't solve my problem but i found the cause of the error. it was in this function:

void cpy(int** arr, int x, int y){
    for(int i = x; i < x + len; i++){
        for(int j = y; j < y + len; j++){
            mat[i][j] = arr[i][j];
        }
    }
}

The mistake is very visible, i corrected it:

void cpy(int** arr, int x, int y){
    for(int i = 0; i < len; i++){
        for(int j = 0; j < len; j++){
            mat[i][j] = arr[i + x][j + y];
        }
    }
}

Although this is very weird. I expected to receive an "array index out of bound" or "segmentation fault" when running the wrong code in the line: mat[i][j] = arr[i][j];

Anyways thank you all for your help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文