C free() function

C free() function - Free allocated memory


void free(void *ptr)

The free() function is used to free a block of storage. The ptr argument points to a block that is previously reserved with a call to the calloc(), malloc(), realloc().


Name Description Required /Optional
ptr Previously allocated memory block to be freed. Required

Return value from free()

  • There is no return value.

Example: free() function

The following example shows the usage of free() function.

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

int main () {
   char *str;

   /* Initial memory allocation */
   str = (char *) malloc(10);
   strcpy(str, "w3resource");
   printf("String = %s,  Address = %u\n", str, str);

   /* Deallocate allocated memory */


String = w3resource,  Address = 7738336

C Programming Code Editor:

Previous C Programming: C calloc()
Next C Programming: C malloc()

Follow us on Facebook and Twitter for latest update.

C Programming: Tips of the Day

What's the point of const pointers?

const is a tool which you should use in pursuit of a very important C++ concept:

Find bugs at compile-time, rather than run-time, by getting the compiler to enforce what you mean.

Even though it does not change the functionality, adding const generates a compiler error when you're doing things you didn't mean to do. Imagine the following typo:

void foo(int* ptr)
    ptr = 0;// oops, I meant *ptr = 0

If you use int* const, this would generate a compiler error because you're changing the value to ptr. Adding restrictions via syntax is a good thing in general. Just don't take it too far -- the example you gave is a case where most people don't bother using const.

Ref : https://bit.ly/33Cdn3Q