【发布时间】:2012-10-29 02:40:04
【问题描述】:
我创建了一个二维数组,但失败了,我得到“lab10.exe 中 0x77a415de 处的未处理异常:0xC0000374:堆已损坏。”不知道从这里去哪里或如何调试。我相信这与我的数组大小或使用 malloc() 有关。非常感谢您提前提供的帮助!
//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 次不同的操作。谢谢!
【问题讨论】:
-
我认为你应该为你的问题添加“c”作为标签
-
我看不出你的分配有什么特别的问题。似乎更有可能在另一点上超出了一个或多个数组 - 一旦分配了这些数组,您可能需要展示一些如何使用这些数组...
标签: arrays c malloc 2d heap-memory