• It is a C library function that helps in freeing the memory.
  • The behavior is undefined if ptr is not a pointer that has been returned by malloc(), calloc() or realloc() or if it has been already deallocated by realloc() or free().
  • It does not return anything.


void free( void *ptr);

where ptr points to the memory to be freed.


int *ptr = (int*) malloc(8);


Once the pointer is freed, it must be set to NULL to avoid any accidental usage.

How does free work or why in free size is not given to be freed?

When we allocate memory, there is a special block that holds the metadata in the pointer variable and this metadata contains size of the memory allocated.

Thus when we use the pointer variable in the free(), we do not need to pass the size of the memory to be freed as malloc or calloc keeps track of size of each block as a metadata.

untitled image

Relevant Posts:

Categories: C Language

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: