Hello.
I am experiencing some problems when trying to free my 2D dynamic array.
The program appears to run perfectly fine if I do not try to free the array, but if I do, the program will freeze at some point with the cursor flashing as if it was waiting for me to input.
I know I should normally check for for return values, but I don't know how to do so. I have google'd but found many different versions of how to do so, so I don't know which one is the right way.
Here is a piece of my code. Please note that there are recursive functions. Could this interfere with freeing the array?
Would be grateful for advice of what's wrong, as well as any eventual code for the "if == NULL" check...
Thanks!
Code:#include <stdio.h> #include <stdlib.h> openTile(int dimY, int dimX, int X, int Y) { int i, j, count,k; char ch; char** minesArray; FILE *MinesFile; FILE *DisplayFile; // ALLOCATING A TWO-DIMENTIONAL DYNAMIC ARRAY FOR BOARD minesArray = (char**)malloc(dimY*sizeof(char*)); for (i = 0; i < dimX; i++) { minesArray[i] = (char*)malloc(dimX * sizeof(char)); } // JUST A CHECK... if (check4Neighbours(dimY,dimX,X,Y) == 0) { if ((X > 0) && (Y > 0)) { // LOAD FILES FROM DISK TO RAM MinesFile = fopen("MinesFile.txt","r"); for (i = 0; i < dimX; i++) { for (j = 0; j < dimY; j++) { minesArray[i][j] = fgetc(MinesFile); } } fclose(MinesFile); if (minesArray[X-1][Y-1] == '0') { // EXECUTE CHANGES minesArray[X-1][Y-1] = '2'; // SAVING DATA TO DISK MinesFile = fopen("MinesFile.txt","w+"); for (i=0;i<dimX;i++) { for (j=0;j<dimY;j++) { fprintf(MinesFile,"%c",minesArray[i][j]); } } fclose(MinesFile); // RECURSING openTile(dimY, dimX, X-1, Y-1); } } ... if (Y > 0) { // LOAD FILES FROM DISK TO RAM MinesFile = fopen("MinesFile.txt","r"); for (i = 0; i < dimX; i++) { for (j = 0; j < dimY; j++) { minesArray[i][j] = fgetc(MinesFile); } } fclose(MinesFile); if (minesArray[X][Y-1] == '0') { // EXECUTE CHANGES minesArray[X][Y-1] = '2'; // SAVING DATA TO FILE MinesFile = fopen("MinesFile.txt","w+"); for (i=0;i<dimX;i++) { for (j=0;j<dimY;j++) { fprintf(MinesFile,"%c",minesArray[i][j]); } } fclose(MinesFile); // RECURSING openTile(dimY, dimX, X, Y-1); } } } // FREEING MEMORY for (i = 0; i < dimY; i++) { free(minesArray[i]); } free(minesArray);



LinkBack URL
About LinkBacks


