CKTIS / C
free — C23 memory function
`free(ptr)` from `<stdlib.h>` deallocates the storage designated by `ptr`, making it available to allocation functions again; a null pointer causes no action and the function returns nothing. A non-null pointer must come from a compatible memory-management function and still designate a live allocation.
Version: C23
Syntax
void free(void *ptr);free(ptr) from <stdlib.h> deallocates the storage designated by ptr, making it available to allocation functions again; a null pointer causes no action and the function returns nothing. A non-null pointer must come from a compatible memory-management function and still designate a live allocation.
The example obtains four int objects with malloc, stores 1 through 4, computes and prints sum 10, and then calls free exactly once. It neither reads the array nor attempts to release it again after deallocation.
Passing an interior pointer, automatic storage, memory from an incompatible allocator, or an already freed allocation has undefined behavior. After free, access through every stale alias is invalid; setting one local pointer to null only protects that alias. C orders deallocation before a later allocation that reuses the same memory region.
Examples
#include <stdio.h>
#include <stdlib.h>
int main(void){int *values=malloc(4*sizeof *values);if(!values)return 1;int sum=0;for(int i=0;i<4;i++){values[i]=i+1;sum+=values[i];}printf("%d\n",sum);free(values);return 0;}
Result
10
The example obtains four `int` objects with `malloc`, stores 1 through 4, computes and prints sum `10`, and then calls `free` exactly once. It neither reads the array nor attempts to release it again after deallocation.