What is Free Pool of Memory in C?

Question: What is a Pool of Free Memory and How is this Used in C?

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,

advertisement
advertisement
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.

advertisement

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.

advertisement
If you wish to look at all C Tutorials, go to C Tutorials.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.