free in C Programming

free()

Next, to release the reserved memory area, you can use the function `free`. This function is declared in `stdlib.h` and has the following form:

 void free (void * ptr); 

The argument ptr specifies the address of a dynamically allocated memory area that you want to free. It's important to note that you should only pass the address of dynamically allocated memory to free. Attempting to free an address outside of dynamically allocated memory or passing NULL to free will result in undefined behavior.

Furthermore, if you attempt to free a memory area that has already been freed, the behavior is unpredictable and should be avoided at all costs.

Once memory has been freed using free, there is no guarantee that the data remains intact in that memory area. Therefore, accessing the same memory area after it has been freed should be avoided to prevent unexpected behavior in your program.

So, an example program that uses functions malloc and free functions in practice.

#include <stdio.h>
#include <stdlib.h>

 int main (void)
 {
	 int * p;
	
	 p = (int *) malloc (sizeof (int)); /* partitioning of type int */ 
	 if (p == NULL) * / failed to reserve area * /
	 {
		 printf ("Failed to allocate space for% d bytes", sizeof (int));
		 return 1;
	 }
	
	 * P = 150;
	 printf ("% d \ n", * p);
	
	 free (p); * / free area * /
	
	 return 0;
 }

In critical applications, it's essential to verify whether memory allocation was successful. This is typically done by checking the return value of the `malloc` function, as demonstrated above. However, in some sample programs and less critical applications, such checks are often omitted. This is because memory shortages on modern computers are rare, though not impossible.

Additionally, once memory is freed, it's crucial to avoid accessing it again. Therefore, it's common practice to immediately set the pointer to NULL after calling the `free` function as a precautionary measure. This helps prevent accidental access to the freed memory region.

free (p); 
 p = NULL;

function free, attempt to free the same area more than once but should not, because nothing happens when you specify a NULL, and that it's safe if you put it like this.

Of course, between the malloc() and the free(), you can do anything with the memory your twisted little heart desires.

Let's see a simple example of the management of malloc and free:

#include <stdio.h>
#include <stdlib.h>

int main() {
    int bytes;
    char *text;

    printf("How many bytes do you want to allocate: ");
    scanf("%d", &bytes);

    text = (char *)malloc(bytes);

    if (text) {
        printf("Reserved memory: %d bytes = %d KB = %d MB\n", bytes, bytes / 1024, bytes / (1024 * 1024));
        printf("The block starts at: %p\n", (void *)text);
        free(text); // Free the allocated memory
    } else {
        printf("Could not allocate memory\n");
    }

    return 0;
}

"This example prompts you to specify how much memory you wish to allocate. Upon successful allocation, it displays the amount of memory reserved and the starting address of the memory block. If the allocation fails, it notifies you with the message: 'Failed to allocate memory.'

Let's consider an example where we reserve 1000 bytes:

How many bytes do you want to allocate: 1000

Reserved Memory: 1000 bytes = 0 KB = 0 MB

The block begins at: 90868

You can experiment with different memory sizes. For instance, if you try to reserve 32 MB of memory:

How many bytes do you want to allocate: 32000000

Could not allocate memory

In this case, `malloc` was unable to reserve the requested amount of memory and returns `NULL`, indicating that the operation could not be performed."

Example:

Ready to get started?

Ready to embark on your journey into the world of C programming? Our comprehensive course provides the perfect starting point for learners of all levels. With engaging lessons, hands-on exercises, and expert guidance, you'll gain the skills and confidence needed to excel in this fundamental programming language. Let's dive in and unlock the endless possibilities of C programming together!