0

Hello everyone!

i am trying to make an ascii tetris in C.

However i am not yet very experienced on pointers so i would like to ask you if these functions i made, allocate and free memory correctly ( meaning that they don't leave memory leaks).

This is the function i call to create the tetris board :

char** InitTetris( int size_x , int size_y )
{
   /* 
      InitTetris allocates memory for the tetris array.
      This function should be called only once at the beginning of the game.
   */  

   //Variables
   int i;
   char** tetris = NULL;

   //Allocate memory
   tetris = ( char** ) malloc ( size_x * sizeof ( char* ) );

   for ( i = 0 ; i < size_x ; i++ )
   {
      tetris[i] = ( char* ) malloc ( size_y * sizeof ( char ) );
   }

   return tetris;

}//End of InitTetris

And this is the function to free the memory :

void ExitTetris ( char** tetris , int size_y )
{
   /*
      This function is called once at the end of the game to
      free the memory allocated for the tetris array.
   */

   //Variables
   int i;

   //Free memory
   for ( i = 0 ; i < size_y ; i++ )
   {
      free( tetris[i] );
   }

   free( tetris );

 }//End of ExitTetris    

Everything handled from another function

void NewGame()
{
   //Variables

   char** tetris;          /* Array that contains the game board       */
   int size_x , size_y;    /* Size of tetris array                     */

   //Initialize tetris array
   tetris = InitTetris( size_x , size_y );

   //Do stuff.....

   //Free tetris array
   ExitTetris( tetris , size_y );

}//End of NewGame

Everything works fine on the program, i just want to make sure that i don't litter peoples RAM ... can you please check my method?

fuz
  • 88,405
  • 25
  • 200
  • 352
Giwrgos Tsopanoglou
  • 1,165
  • 2
  • 8
  • 15
  • 1
    Stack Overflow isn't a code evaluation site. Check with colleagues or fellow students as the case may be. Perhaps they'll have input. – Tom Jan 08 '13 at 23:43
  • What operating system are you using? If you're on Windows, I'd suggest acquiring a copy of windbg and learning how to use it. It can help you track down memory leaks in your code. Some tutorials can be found at http://cfc.kizzx2.com/index.php/detecting-memory-leaks-with-windbg-the-modern-and-free-way/ and http://cprogrammers.blogspot.com/2006/09/windows-memory-leak-analysis-using.html – E.Z. Hart Jan 08 '13 at 23:45
  • 3
    And if on a unix-alike system consider [valgrind](http://valgrind.org/). – dmckee --- ex-moderator kitten Jan 08 '13 at 23:46
  • i am in high school... noone does C... @Tom – Giwrgos Tsopanoglou Jan 08 '13 at 23:48
  • Thanks for the link @E.Z.Hart ... i am using windows – Giwrgos Tsopanoglou Jan 08 '13 at 23:49
  • You have passed the wrong dimension to `ExitTetris`, Giωrgos, you should pass `size_x`, since that's the number of `char*` you allocate. – Daniel Fischer Jan 09 '13 at 00:48

4 Answers4

2

Your best friend for memory leaks is a good guide to C programming. There are lots of those available. After that consider some tools like valgrind or efence (linux),

http://valgrind.org/

efence comes with some linux distributions.

Windows has tools for heap analysis too, for example on XP:

http://support.microsoft.com/kb/268343

jim mcnamara
  • 16,005
  • 2
  • 34
  • 51
0

It seems to be ok. It is good practice to check if malloc does not return NULL while creating memory.

hmatar
  • 2,437
  • 2
  • 17
  • 27
0

I think you're ok. Just what @Hassan Matar said, it is worth doing. One other thing, though. I learned here, in stackoverflow, not casting your mallocs is a better alternative to casting them.

The number of possible errors you can get for telling your compiler that you (probably) know the data type you're working is not worth the risk.

Check this question for more detailed information.

Community
  • 1
  • 1
Afonso Tsukamoto
  • 1,184
  • 1
  • 12
  • 21
0

Check to see whether any of the memory couldn't be allocated, because if the memory was never allocated, the deallocation can cause a crash:

//In InitTetris:
tetris = ( char** ) malloc ( size_x * sizeof ( char* ) );

//if tetris == NULL, then exit from the function with a failure code
//and don't continue the rest of the program.
//

For more information on the possibility of malloc failing, see this URL:

How detect malloc failure?

Community
  • 1
  • 1
A B
  • 4,068
  • 1
  • 20
  • 23