C free() Function

Beginner
7 min read
Updated: Nov 2026
3 examples
<stdlib.h>

Definition

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);
CallWhat 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

ParameterDescription
ptrPointer returned by an allocation function, or NULL.

Return Value

Return ValueDescription
No valuefree() 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;
}

How It Works

  1. Step 1. Allocate three integers and check the pointer.
  2. Step 2. Write and read while the allocation is alive.
  3. Step 3. Pass the original allocation pointer to free().
  4. 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;
}

How It Works

  1. Step 1. Initialize the pointer to NULL.
  2. Step 2. Pass NULL to free().
  3. Step 3. The standard defines this as a no-op.
  4. 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.

C
#include <stdio.h>
#include <stdlib.h>

int main(void) {
    char *owner = malloc(16);
    if (!owner) return EXIT_FAILURE;
    owner[0] = 'A'; owner[1] = '\0';
    printf("%s\n", owner);
    free(owner);
    return 0;
}

How It Works

  1. Step 1. Keep one pointer as the allocation owner.
  2. Step 2. Initialize a valid small C string.
  3. Step 3. Use the data before ending its lifetime.
  4. 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.

Next: getenv()

Continue through the C stdlib.h tutorials.

getenv() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful