The C free() function releases dynamically allocated storage. Passing NULL does nothing. Any other pointer must identify a live block returned by malloc(), calloc(), realloc(), or aligned_alloc(), and not already have been freed.
This function is declared in <stdlib.h>. Its contract matters as much as its call: validate every required precondition, check any documented sentinel, and keep ownership or host effects clear in the surrounding code. A valid live allocation is released; free(NULL) is also harmless. No failure value exists. Invalid, interior, stack, or already-freed pointers cause undefined behavior. The examples below move from the direct call to an edge condition and a practical use.
Syntax
Start with this concrete free() call:
C
free(buffer);
Call
What it does
free(buffer);
Releases dynamically allocated storage using the concrete values shown.
Include <stdlib.h>. Also include headers that declare any types, limits, locale tools, or I/O functions used around the call.
Library declaration
C
void free(void *ptr);
Parameters
Parameter
Description
ptr
Pointer returned by an allocation function, or NULL.
Return Value
Return Value
Description
No value
free() returns void.
C Examples
Example 1 — Release an array
This complete program demonstrates a distinct part of the free() contract and checks the result or precondition that matters for the scenario.
C
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int *p = malloc(3 * sizeof *p);
if (!p) return EXIT_FAILURE;
p[0] = 7;
printf("%d\n", p[0]);
free(p);
p = NULL;
return 0;
}
Output
7
How It Works
Step 1. Allocate three integers and check the pointer.
Step 2. Write and read while the allocation is alive.
Step 3. Pass the original allocation pointer to free().
Step 4. Clear the local pointer to avoid accidental reuse.
Example 2 — Free NULL safely
This complete program demonstrates a distinct part of the free() contract and checks the result or precondition that matters for the scenario.
C
#include <stdlib.h>
int main(void) {
int *p = NULL;
free(p);
return EXIT_SUCCESS;
}
Output
(no output)
How It Works
Step 1. Initialize the pointer to NULL.
Step 2. Pass NULL to free().
Step 3. The standard defines this as a no-op.
Step 4. The program finishes normally.
Example 3 — Clean up one owner
This complete program demonstrates a distinct part of the free() contract and checks the result or precondition that matters for the scenario.
Step 4. Free exactly once through the owner pointer.
Frequently Asked Questions
free() releases dynamically allocated storage. Passing NULL does nothing. Any other pointer must identify a live block returned by malloc(), calloc(), realloc(), or aligned_alloc(), and not already have been freed.
Include <stdlib.h> before calling free().
A valid live allocation is released; free(NULL) is also harmless. No failure value exists. Invalid, interior, stack, or already-freed pointers cause undefined behavior.
After free(ptr), the pointer value is indeterminate for dereferencing purposes. Do not use or free the allocation again; setting your variable to NULL can prevent accidental reuse.
The function is specified by the C standard library. Exact limits, locale behavior, environment behavior, allocation capacity, or host effects can still vary where relevant.