I'm having an issue with dynamically creating a "multi-dimensional array". I've read 6.14 on the comp.lang.c FAQ, and am following the code that was listed there.
cache_array = malloc(cm_blks * sizeof(int *));
if (cache_array = NULL) {
fprintf(stderr, "out of memory\n");
exit(1);
}
for (i = 0; i < cm_blks; i++) {
cache_array[i] = malloc(6 * sizeof(int));
if (cache_array[i] == NULL) {
fprintf(stderr, "out of memory\n");
exit(1);
}
}
The variable cm_blks is an integer, in my test case equal to 8. cache_array is initialized as:
int **cache_array;
The code compiles fine, but I get a segmentation fault at the second malloc line when I run the output.