C free()

BrandonDusch's avatar
Published Aug 18, 2022
Contribute to Docs

The free() function is used to dynamically de-allocate the memory at runtime. Memory allocated by the malloc() and calloc() functions must be manually de-allocated when not in use. Freeing it helps to reduce memory wastage.

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours

Syntax

free(memoryblock);

The memoryblock must exist beforehand in order to be de-allocated by the free() function.

Example

The following example showcases the free() function:

int *ptr;
// Memory allocated using malloc() function
ptr = (int*) malloc(5 * sizeof(int));
// Memory de-allocated using free() function
free(ptr);

All contributors

Contribute to Docs

Learn C on Codecademy

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours