logo

Good practice for freeing memory in C

#include <stdio.h> #include <stdlib.h> #include <string.h> void process_arr(int** arr, int n) { // processing arr free(*arr); *arr = NULL; } int main(int argc, char* argv[]) { int* arr = malloc(sizeof(int) * 10); process_arr(&arr, 10); if (arr == NULL) { // .... arr has been freed } free(arr); // if arr is NULL free() simply ignores it arr = NULL; return 0; }
Course thumb

The C programming language made simple