Dear C experts,
The below program compiles without any error and prints values as expected.
However, dumps core. I could do this without dumping core on a int *i, however, int **i is giving a core dump.
I would much appreciate if anyone could provide their views on dynamic allocation of such arrays.
The same thing on a char with same sizeof(int) works without a core dump.
Thanks
Nethaji
Code:
#include <stdio.h>
#include <malloc.h>
int main()
{
int **i, j, k;
i = calloc(10, sizeof(int));
for(j = 0; j < 10; j++)
{
i[j] = calloc(30, sizeof(int));
}
for(j = 0; j < 10; j++)
{
for(k = 0; k < 30; k++)
{
printf("%d == ", k);
i[j][k] = (k + 10);
printf("%d\n", i[j][k]);
}
}
/*Free memory:*/
for(j = 0; j < 10; j++)
{
free(i[j]);
}
free(i);
}