The free function in C is used to free or evict memory that was previously dynamically allocated using functions such as malloc, calloc, or realloc. This helps prevent memory leaks by allowing the operating system to reclaim memory space that is no longer needed.

free(ptr);
free(str->field);

It is very important not to call free if you did not call malloc (like on an array[]) otherwise C will throw an error

Good practices:

  • Assigns NULL to a pointer after freeing it to avoid double freeing (If the ptr pointer is NULL, the free function does nothing)