Problem with freeing 2D array

This is a discussion on Problem with freeing 2D array within the C Programming forums, part of the General Programming Boards category; Hello. I am experiencing some problems when trying to free my 2D dynamic array. The program appears to run perfectly ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    7

    Problem with freeing 2D array

    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);
    Last edited by dcdude; 04-27-2011 at 06:13 AM.

  2. #2
    Registered User
    Join Date
    Apr 2011
    Location
    Bangalore
    Posts
    20
    Code:
    minesArray = (char**)malloc(dimY*sizeof(char*));
    look at the above section of code.
    You are allocating memory dimy elements but you are only creating dimx string
    Code:
    for (i = 0; i < dimX; i++)
    	{  
    		minesArray[i] = (char*)malloc(dimX * sizeof(char));
    	}
    Now while freeing, you are try to free dimY string. if dimY > dimX then this program crash.
    Code:
    // FREEING MEMORY
            for (i = 0; i < dimY; i++)
    	{
    		free(minesArray[i]);
    	}
    	free(minesArray);

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    7
    Hi Shashi.

    Thank you for your reply.

    I changed dimY to dimX when freeing, using the same values as I used before (5 and 6), but the results were the same. The program froze.

    What could be wrong?

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Make sure you're not overflowing your heap.
    And use a debugger!!

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    90
    The dimY when freeing wasn't the problem. The dimX while mallocing is.

  6. #6
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Did you read ShashiKantSuman post?
    Read again carefully.

    Code:
           minesArray = (char**)malloc(dimY*sizeof(char*));
    
           for (i = 0; i < dimX; i++)
    	{  
    		minesArray[i] = (char*)malloc(dimX * sizeof(char));
    	}
    What will happen if dimX > dimY ?

  7. #7
    Registered User
    Join Date
    Apr 2011
    Posts
    7
    Yeah, I just seen the problem... silly me. Thanks anyway for your replies all...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. allocation and freeing of an array of ints
    By boxden in forum C++ Programming
    Replies: 4
    Last Post: 10-15-2010, 03:30 PM
  2. Back To The Basics: Freeing An Array on the Heap?
    By Deo in forum C++ Programming
    Replies: 12
    Last Post: 04-07-2007, 04:42 AM
  3. freeing problem with concurrent processes
    By alavardi in forum C Programming
    Replies: 2
    Last Post: 03-07-2005, 12:09 PM
  4. freeing dynamic 2D array
    By GaPe in forum C Programming
    Replies: 3
    Last Post: 06-28-2003, 04:43 AM
  5. freeing an array of structures
    By mackol in forum C Programming
    Replies: 6
    Last Post: 06-03-2002, 11:43 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21