>> | Okay so I ran your code through a debugger and figured out what happened. Also as one minor thing I think you forgot your #include's for printf and calloc (which I believe are <stdio.h> and <stdlib.h> respectively).
So you're using calloc() which allocates you cleaned memory that is set to all zeros. The NULL pointer is also represented by all zeroes. You should consider how the constructs you are invoking actually work here. What you're allocating with calloc() is a block of memory (in this case, a block of memory of size "RADIUS * RADIUS * sizeof(int)" ) and setting that memory to all zeroes. Then your code, outside of calloc, is creating an int** and pointing it to that newly formed block of zeroed memory.
If you were to access this block of memory as a regular int* array, like this: int* memory = calloc(RADIUS * RADIUS, sizeof(int) ); for (int x = 0; x < RADIUS * RADIUS; x++) printf("%d,", memory[x]); Then that would print out RADIUS * RADIUS zeroes, since all of the ints in your block of memory are zero.
Now what you did instead was you made a pointer-array pointing to that block of all zeroed memory. We can indeed print out the pointer values of each element in your array like this: mask = (int**)calloc(RADIUS * RADIUS, sizeof(int) ); for (int x = 0; x < RADIUS; ++x) { int* thisPointer = mask[x]; printf("%p,", thisPointer); }
The code will now print RADIUS NULL pointers in a row. The reason that all of the pointers in this array are NULL is because in almost all C or C++ compiler implementations, the NULL pointer is equivalent to a pointer to address zero (which is then mapped in most CPU hardware to cause an access violation exception, which stops your program with a crash). What your original crashing code was doing was trying to dereference the NULL pointers in the array of pointers that you made.
Now you can fix your crashing code in several different ways. I'll show you two of them here:
// Fix solution #1 // Allocate arrays of arrays /* The only change is to how you allocate the arrays in main(), so I won't bother rewriting your printMatrix() function here */ nt main(int argc, char** args) { printf("%d\n", RADIUS * RADIUS); // mask = (int**) calloc(RADIUS * RADIUS, sizeof(int) ); mask = (int**)calloc(RADIUS, sizeof(int*) ); for (int x = 0; x < RADIUS; x++) { mask[x] = (int*)calloc(RADIUS, sizeof(int) ); /* Initialize our pointers rather than leaving them all pointing at NULL */ } // createMask(mask, RADIUS); printMatrix(mask, RADIUS); exit(0); }
// Fix solution #2 // Keep the original block allocation the same, but use a different addressing scheme for access #define RADIUS 7 int* mask; // Changed mask from an int** to an int*, you'll see why in a sec... void printMatrix(int* matrix, int size) { for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { printf("%d", matrix[j + i * size]); // Change how we're addressing things } printf("\n"); } }
int main(int argc, char** args) { printf("%d\n", RADIUS * RADIUS); mask = (int*)calloc(RADIUS * RADIUS, sizeof(int) ); // Changed mask from an int** to an int* printMatrix(mask, RADIUS); exit(0); return 0; }
I hope that all of this helps, and if you have any further questions, please don't hesitate to ask and I'll try to explain it the best I can! :) |