free -- Free a Block of Memory


SYNOPSIS
#include <stdlib.h>
void free(void *block);
DESCRIPTION
free
frees a block of memory previously allocated by malloc
or
calloc
. block
is a pointer to the memory block.
RETURN VALUE
free
has no return value.
ERRORS
User ABEND 1206, 1207, or 1208 may occur if memory management data
areas are overlaid. User ABEND 1208 will probably occur if the block
pointer is invalid; that is, if it does not address a previously
allocated area of memory that has not already been freed.
IMPLEMENTATION
If an entire page of memory is unused after a free
call, the page
is returned to the operating system unless the page is included in the
initial heap area whose size can be specified with a run-time argument.
EXAMPLE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct LIST
{
struct LIST *next;
char text[0]; /* Zero-length arrays are a SAS/C extension. */
};
main(int argc, char *argv[])
{
struct LIST *p;
struct LIST *q;
struct LIST list;
char str[256];
int size;
while (1){
puts("nBegin new group...");
for (q = &list; ; q = p){
puts("Enter a text string: ");
if (fgets(str,sizeof(str),stdin) == NULL){
break;
}
if (str[0] == '0'){
if (q == &list)
exit(EXIT_SUCCESS);
break;
}
size = sizeof(struct LIST) + strlen(str) + 1;
p = (struct LIST *)malloc(size);
if (p == NULL){
puts("No more memory");
exit(EXIT_FAILURE);
}
q->next = p;
p->next = NULL;
strcpy(p->text, str);
}
puts("nnTEXT LIST...");
/* Be sure to copy the next pointer from */
/* the current block before you free it. */
p = list.next;
while(p != NULL){
q = p->next;
printf(p->text);
free((char *)p);
p = q;
}
list.next = NULL;
exit(EXIT_SUCCESS);
}
}
RELATED FUNCTIONS
pfree
SEE ALSO
Memory Allocation Functions