cplusplus.com cplusplus.com
cplusplus.com   C++ : Reference : C Library : cstdlib (stdlib.h) : free
 
- -
C++
Information
Documentation
Reference
Articles
Sourcecode
Forum
Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
C Library
cassert (assert.h)
cctype (ctype.h)
cerrno (errno.h)
cfloat (float.h)
climits (limits.h)
clocale (locale.h)
cmath (math.h)
csetjmp (setjmp.h)
csignal (signal.h)
cstdarg (stdarg.h)
cstddef (stddef.h)
cstdio (stdio.h)
cstdlib (stdlib.h)
cstring (string.h)
ctime (time.h)
cstdlib (stdlib.h)
functions:
· abort
· abs
· atexit
· atof
· atoi
· atol
· bsearch
· calloc
· div
· exit
· free
· getenv
· labs
· ldiv
· malloc
· mblen
· mbstowcs
· mbtowc
· qsort
· rand
· realloc
· srand
· strtod
· strtol
· strtoul
· system
· wcstombs
· wctomb
functions (non-standard):
· itoa
macros:
· EXIT_FAILURE
· EXIT_SUCCESS
· MB_CUR_MAX
· NULL
· RAND_MAX
types:
· div_t
· ldiv_t
· size_t

-

free function
void free ( void * ptr );
<cstdlib>

Deallocate space in memory

A block of memory previously allocated using a call to malloc, calloc or realloc is deallocated, making it availbale again for further allocations.

Parameters

ptr
Pointer to a memory block previously allocated with malloc, calloc or realloc to be deallocated.
If a null pointer is passed as argument, no action occurs.

Return Value

(none)

Example

/* free example */
#include <stdio.h>
#include <stdlib.h>

int main ()
{
  int * buffer1, * buffer2, * buffer3;
  buffer1 = (int*) malloc (100*sizeof(int));
  buffer2 = (int*) calloc (100,sizeof(int));
  buffer3 = (int*) realloc (buffer2,500*sizeof(int));
  free (buffer1);
  free (buffer3);
  return 0;
}

This program has no output. Just demonstrates some ways to allocate and free dynamic memory using the cstdlib functions.

See also

malloc Allocate memory block (function)
calloc Allocate space for array in memory (function)
realloc Reallocate memory block (function)
© The C++ Resources Network, 2000-2007 - All rights reserved
Spotted an error? - contact us