The free() Call

Let's learn about the `free` call in this lesson!

As it turns out, allocating memory is the easy part of the equation; knowing when, how, and even if to free memory is the hard part. To free heap memory that is no longer in use, programmers simply call free():

Press + to interact
#include<stdio.h>
int main() {
int *x = malloc(10 * sizeof(int));
printf("%d\n", sizeof(x));
//...
free(x);
return 0;
}

The routine takes one argument, a pointer returned by malloc(). Thus, you might notice, the size of the allocated region is not passed in by the user, and must be tracked by the memory-allocation library itself.

Get hands-on with 1400+ tech skills courses.