Format
#include <stdlib.h> void free(void *ptr);
Language Level: ANSI
Threadsafe: Yes.
Description
The free() function frees a block of storage. The ptr argument points to a block that is previously reserved with a call to the calloc(), malloc(), realloc(), _C_TS_calloc(), _C_TS_malloc(), _C_TS_realloc(), or _C_TS_malloc64() functions. The number of bytes freed is the number of bytes specified when you reserved (or reallocated, in the case of the realloc() function) the block of storage. If ptr is NULL, free() simply returns.
To use Teraspace storage instead of heap storage without changing the C source code, specify the TERASPACE(*YES *TSIFC) parameter on the CRTCMOD compiler command. This maps the free() library function to _C_TS_free(), its Teraspace storage counterpart.
If a C2M1212 message is signalled and the data area QGPL/QC2M1212 exists, the program stack at the point that the C2M1212 message is signalled is spooled to a spool file. You can create the QGPL/QC2M1212 data area using the CRTDTAARA (Create Data Area) command. You can specify any type and length for the data area.
Following are some of the problems that can cause a C2M1212 message to be signalled:
When a C2M1212 message is generated, the hexadecimal value of the pointer passed to the free() function is included as part of the message description. This hexadecimal value can provide clues as to the origin of the problem. The malloc() function returns only pointers that end in hexadecimal 0. Any pointer that does not end in hexadecimal 0 was either never set to point to storage reserved by the malloc() function or was modified since it was set to point to storage reserved by the malloc() function. If the pointer ends in hexadecimal 0, then the cause of the C2M1212 message is uncertain, and the program code that calls free() should be examined.
Return Value
There is no return value.
Example that uses free()
This example uses the calloc() function to allocate storage for x array elements, and then calls the free() function to free them.
#include <stdio.h> #include <stdlib.h> int main(void) { long * array; /* start of the array */ long * index; /* index variable */ int i; /* index variable */ int num; /* number of entries of the array */ printf( "Enter the size of the array\n" ); scanf( "%i", &num ); /* allocate num entries */ if ( (index = array = calloc( num, sizeof( long ))) != NULL ) { for ( i = 0; i < num; ++i ) /* put values in array */ *index++ = i; /* using pointer notation */ free( array ); /* deallocates array */ } else { /* Out of storage */ perror( "Error: out of storage" ); abort(); } }
Related Information
(C) Copyright IBM Corporation 1992, 2006. All Rights Reserved.