构建成功,但有堆corupted。使用2-D阵列 [英] Build Succeeds, But have heap corupted. Using 2-D Arrays

查看:168
本文介绍了构建成功,但有堆corupted。使用2-D阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个2-D数组,但失败,我得到未处理的异常在0x77a415de在lab10.exe:0xC0000374:堆已损坏不知道从哪里从这里或如何调试去。我相信这是与我的数组的大小或使用malloc()。非常感谢你的帮助提前!

I've created a 2-D Array, but fails and I get "Unhandled exception at 0x77a415de in lab10.exe: 0xC0000374: A heap has been corrupted." Not sure where to go from here or how to debug. I believe it has something to do with the size of my arrays or using malloc(). Thank you so much for the help in advance!

//Get the number of Columns from the user
printf("Enter the number of rows and columns:\n");
scanf("%d", &dimension);

//Allocate 1-D Array of Pointers, Array 'a'
a = (int** ) malloc( sizeof(int)*dimension);
if(a == NULL)
{
    printf("\nError Allocating Memory");
    exit(1);
}

//Allocate Rows for 2-D Array; Array 'a'
for(r = 0; r < dimension; r++)
{
    a[r] = (int*) malloc( sizeof(int) * dimension);
    if (a[r] == NULL)
    {
        for(i = 0; i< r; i++)
            free(a[i]);
        printf("\nError Allocating Memory");
        exit(1);
    }
}

还有更多,但我做这4个不同的时间从同一个整数'尺寸'。谢谢!

There is more, but I do this 4 different times from the same integer 'dimension'. Thank you!

推荐答案

我需要时间,我编译你的第一个例子,它运行良好,我的事情了错误,则可能是 A 的类型必须为 INT ** A 。另外我有给类型添加到研究变量。如果忽略了最低的工作,那么我认为你必须指定错误的行,并添加变量声明,以及#包括。希望这可以帮助...

I take a time and i compile your first example and it runs fine, i thing that the error then could be that a must be of type int **a. Also i have to add the types to r and i variables. If this dosen't works, then i think that you must specify the line of the error, and also add the variables declarations, and the #includes. Hope this could helps...

这篇关于构建成功,但有堆corupted。使用2-D阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆