请告诉我有什么问题。
创建二维动态数组:我创建了一个指针数组,然后为每个指针分配了一个块。
这是代码:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int i, j;
int **a = NULL;
a = (int **)malloc(5*sizeof(int *));
if ( NULL == a)
{
printf("Failed to allocate memory.");
return 1;
}
for ( i = 0; i < 10; i++ )
a[i] = (int *)malloc(10*sizeof(int ));
for ( i = 0; i < 5; i++ )
{
for ( j = 0; j < 10; j++ )
a[i][j] = i*j;
}
for ( i = 0; i < 5; i++ ) // While running it prints this array. But...
{
for ( j = 0; j < 10; j++ )
printf("%4d", a[i][j]);
printf("\n");
}
/* Trying to free allocated memory. */
for ( i = 0; i < 5; i++ ) // ... sometimes app crashes around here.
{
free(a[i]);
}
return 0;
}
看不到这种未定义行为的任何原因......