Free() Function in C++

How is free() used in C++?

Explanation

Free() Function is used to free the memory pointed by the pointer back to the memory heap. This function should be called on a pointer that was used either with "calloc()" or "malloc()", otherwise the function will destroy the memory management making a system to crash.

Syntax:


void *free(void *ptr);

Example :


#include <stdlib.h>
#include <stdio.h>
int main ()
{
int * ptr;
ptr = (int*) malloc (80*sizeof(int));
free(ptr);
return(0);
}

In the above example the first the memory is allocated using the "malloc()" then using the free() function the memory is freed.

C++ Tutorial


Ask Questions

Ask Question