December 2005
Beginner to intermediate
618 pages
20h 19m
English
free
Releases allocated memory
#include <stdlib.h> voidfree( void *ptr);
After you have finished using a memory block that you
allocated by calling malloc(),
calloc() or realloc(), the free() function releases it to the system
for recycling. The pointer argument must be the exact address
furnished by the allocating function, otherwise the behavior is
undefined. If the argument is a null pointer, free() does nothing. In any case, free() has no return value.
char *ptr; /* Obtain a block of 4096 bytes ... */ ptr =calloc(4096, sizeof(char)); if ( ptr == NULL ) fprintf( stderr, "Insufficient memory.\n" ), abort(); else { /* ... use the memory block ... */ strncpy( ptr, "Imagine this is a long string.\n", 4095 ); fputs( stdout, ptr ); /* ... and release it. */free( ptr ); }
Read now
Unlock full access