Note that there are some explanatory texts on larger screens.

plurals
  1. POCheck for proper memory deallocation in C
    primarykey
    data
    text
    <p>Hello everyone!</p> <p>i am trying to make an ascii tetris in C. </p> <p>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).</p> <p>This is the function i call to create the tetris board :</p> <pre><code>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 &lt; size_x ; i++ ) { tetris[i] = ( char* ) malloc ( size_y * sizeof ( char ) ); } return tetris; }//End of InitTetris </code></pre> <p>And this is the function to free the memory :</p> <pre><code>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 &lt; size_y ; i++ ) { free( tetris[i] ); } free( tetris ); }//End of ExitTetris </code></pre> <p>Everything handled from another function</p> <pre><code>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 </code></pre> <p>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?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload