![]() Chapter Contents |
![]() Previous |
![]() Next |
| free |
| Portability: | ISO/ANSI C conforming, UNIX compatible |
| SYNOPSIS | |
| DESCRIPTION | |
| RETURN VALUE | |
| ERRORS | |
| IMPLEMENTATION | |
| EXAMPLE | |
| RELATED FUNCTIONS | |
| SEE ALSO |
| 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 |
| 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("\n\nTEXT 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 |
| SEE ALSO |
![]() Chapter Contents |
![]() Previous |
![]() Next |
![]() Top of Page |
Copyright © 2001 by SAS Institute Inc., Cary, NC, USA. All rights reserved.