Using free() Function in C
By: Daniel Malcolm in C Tutorials on 2007-10-03
When you allocate memory with either malloc() or calloc(), it is taken from the dynamic memory pool that is available to your program. This pool is sometimes called the heap, and it is finite. When your program finishes using a particular block of dynamically allocated memory, you should deallocate, or free, the memory to make it available for future use. To free memory that was allocated dynamically, use free(). Its prototype is
void free(void *ptr);
The free() function releases the memory pointed to by ptr. This memory must have been allocated with malloc(), calloc(), or realloc(). If ptr is NULL, free() does nothing. Sample program below demonstrates the free() function.
Using free() to release previously allocated dynamic memory.
1: /* Using free() to release allocated dynamic memory. */ 2: 3: #include <stdio.h> 4: #include <stdlib.h> 5: #include <string.h> 6: 7: #define BLOCKSIZE 30000 8: 9: main() 10: { 11: void *ptr1, *ptr2; 12: 13: /* Allocate one block. */ 14: 15: ptr1 = malloc(BLOCKSIZE); 16: 17: if (ptr1 != NULL) 18: printf("\nFirst allocation of %d bytes successful.",BLOCKSIZE); 19: else 20: { 21: printf("\nAttempt to allocate %d bytes failed.\n",BLOCKSIZE); 22: exit(1); 23: } 24: 25: /* Try to allocate another block. */ 26: 27: ptr2 = malloc(BLOCKSIZE); 28: 29: if (ptr2 != NULL) 30: { 31: /* If allocation successful, print message and exit. */ 32: 33: printf("\nSecond allocation of %d bytes successful.\n", 34: BLOCKSIZE); 35: exit(0); 36: } 37: 38: /* If not successful, free the first block and try again.*/ 39: 40: printf("\nSecond attempt to allocate %d bytes failed.",BLOCKSIZE); 41: free(ptr1); 42: printf("\nFreeing first block."); 43: 44: ptr2 = malloc(BLOCKSIZE); 45: 46: if (ptr2 != NULL) 47: printf("\nAfter free(), allocation of %d bytes successful.\n", 48: BLOCKSIZE); 49: return(0); 50: } First allocation of 30000 bytes successful. Second allocation of 30000 bytes successful.
AANALYSIS: This program tries to dynamically allocate two blocks of memory. It uses the defined constant BLOCKSIZE to determine how much to allocate. Line 15 does the first allocation using malloc(). Lines 17 through 23 check the status of the allocation by checking to see whether the return value was equal to NULL. A message is displayed, stating the status of the allocation. If the allocation failed, the program exits. Line 27 tries to allocate a second block of memory, again checking to see whether the allocation was successful (lines 29 through 36). If the second allocation was successful, a call to exit() ends the program. If it was not successful, a message states that the attempt to allocate memory failed. The first block is then freed with free() (line 41), and a new attempt is made to allocate the second block.
You might need to modify the value of the symbolic constant BLOCKSIZE. On some systems, the value of 30000 produces the following program output:
First allocation of 30000 bytes successful. Second attempt to allocate 30000 bytes failed. Freeing first block. After free(), allocation of 30000 bytes successful.
On systems with virtual memory, of course, allocation will always succeed.
DO free allocated memory when you're done with it.DON'T assume that a call to malloc(), calloc(), or realloc() was successful. In other words, always check to see that the memory was indeed allocated.
Add Comment
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
- Your name, rating, website address, town, country, state and comment will be publicly displayed if entered.
- Aside from the data entered into these form fields, other stored data about your comment will include:
- Your IP address (not displayed)
- The time/date of your submission (displayed)
- Your email address will not be shared. It is collected for only two reasons:
- Administrative purposes, should a need to contact you arise.
- To inform you of new comments, should you subscribe to receive notifications.
- A cookie may be set on your computer. This is used to remember your inputs. It will expire by itself.
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
- Although the administrator will attempt to moderate comments, it is impossible for every comment to have been moderated at any given time.
- You acknowledge that all comments express the views and opinions of the original author and not those of the administrator.
- You agree not to post any material which is knowingly false, obscene, hateful, threatening, harassing or invasive of a person's privacy.
- The administrator has the right to edit, move or remove any comment for any reason and without notice.
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
- Data Science
- Android
- React Native
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
Sum of the elements of an array in C
Printing a simple histogram in C
Find square and square root for a given number in C
Simple arithmetic calculations in C
Passing double value to a function in C
Passing pointer to a function in C
Infix to Prefix And Postfix in C
while, do while and for loops in C
Comments