free_arr - 1Fr3aK2/Cub3d GitHub Wiki
📝 free_arr
This function frees all the memory allocated for a 2D array of strings, including the individual strings and the array itself.
⚙️ Parameters
| Parameter | Type | Description |
|---|---|---|
arr |
char** |
A pointer to the array of strings (array of char pointers). |
🔁 Returns
| Return value | Description |
|---|---|
void |
This function does not return a value. It frees memory. |
📖 Description
The free_arr function is used to release the memory occupied by a 2D array of strings. It frees the memory for each string in the array and then frees the memory for the array itself.
- The function starts by checking if the array is
NULL. If it is, it returns immediately without doing anything. - It loops through the array, freeing each string and setting the pointer to
NULLto avoid dangling pointers. - After freeing all individual strings, it frees the array itself and sets the pointer to
NULL.
Note: This function does not check for double freeing. Ensure that the array and strings are allocated properly before calling this function.
💡 Example Usage
char *arr[] = {"Hello", "world", "42", NULL}; // Example array of strings
int height = 3; // Number of strings
free_arr(arr); // Frees the memory occupied by the array and strings
// After calling free_arr, arr and each element of arr are set to NULL