Answer: Two processes malloc() and free() maintain available memory in the System as pool of free memory for use by processes requiring dynamic allocation. When a program requests for dynamic allocation, malloc() attempts to allocate requested block of memory from this pool. If memory allocation is successful, program uses this chunk and after it’s done frees this up back to the pool by calling free() function.
If for reasons, say pool doesn’t have enough memory, malloc() may request the O.S. to arrange the requested chunk, O.S. tries to free up memory to this pool. But if O.S., too, failed, memory allocation to program gets failed. In this case, malloc() returns NULL pointer. Therefore, it’s mandatory to verify whether pointer returned by malloc() isn’t NULL before using it. Be careful not to dereference the NULL pointer!
You can write a subroutine to verify pointer returned by malloc(). Let’s consider a simple program,
/* * malloc_free.c -- Let's allocate memory dynamically and free that * up for reuse */ #include <stdio.h> #include <stdlib.h> /* declarations for malloc(), free() etc. */ int main(void) { int *pi, i, nbytes = 0; /* by default nbytes value 0 */ void *p2v; puts("\nLet's try to get memory chunk, specify number of bytes" " u want...\n"); scanf("%d", &nbytes); pi = malloc(nbytes); printf("Now, we verify if required %d bytes allocated" " successfully.\n", nbytes); if (pi == NULL) { puts("Error: Pool short of memory!"); exit(1); } else { puts("Yes Succeeded!\n"); } puts("Good! Now, let's use dynamic memory...\n"); printf("User type in %d integers one by one...\n", nbytes / sizeof(int)); for (i = 0; i < nbytes / sizeof(int); i++) scanf("%d", (pi + i)); puts(""); printf("User, let's see what had u typed...\n"); for (i = 0; i < nbytes / sizeof(int); i++) printf("%d ", *(pi + i)); puts(""); puts("Let's return allocated memory to system for reuse."); free(pi); /* free up memory */ puts("Thank you!\n"); return 0; }
Let’s observe the output,
Let's try to get memory chunk, specify number of bytes u want... 20 Now, we verify if required 20 bytes allocated successfully. Yes Succeeded! Good! Now, let's use dynamic memory... User type in 5 integers one by one... 23 12 432 34 22 User, let's see what had u typed... 23 12 432 34 22 Let's return allocated memory to system for reuse. Thank you!
The syntax of malloc() and free() functions,
void *malloc(size_t num_of_bytes); void free(void *);
When memory allocation is requested using malloc(), it tries to allocate the requested chunk and returns pointer of type void to the beginning of that chunk. We know that pointer to void can be converted to any other type. And memory allocated then be used by the program. One more thing is that memory allocated by malloc() is in no way initialized.
Further, malloc() allocates specified amount of memory as no. of bytes. So, in order to obtain memory for 10 integers, we better write as
int n = 10; int *pi; pi = (int *)malloc(n * sizeof(int));
Notice that malloc() returned type void * which converted to type int *. sizeof operator used to evaluate size of type int on a Linux machine which we multiplied by n to evaluate memory for 10 integers. The benefit obtained here is program is more portable as size of type int varies on different implementations.
After you are done with the allocation and no longer want to use it, return the block back to pool of free memory, by calling free(), for re-allocation to other part of your program or use by other process.
void free(pi);
If you didn’t return memory back to the pool immediately after you were no longer needed it, it would return to the pool when program exits. But till then program retained the block and so why it’s of no use to other processes.
Sanfoundry Global Education & Learning Series – 1000 C Tutorials.
- Get Free Certificate of Merit in C Programming
- Participate in C Programming Certification Contest
- Become a Top Ranker in C Programming
- Take C Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Apply for C Internship
- Buy C Books
- Buy Computer Science Books
- Apply for Computer Science Internship
- Practice BCA MCQs