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?