malloc() and free() Function in C

Notes:

malloc() and free() Function in C Programming Language:

malloc() function :
- malloc stands for memory allocation
- It allocates a single memory block of a given size in bytes
- The allocated memory block contains collection of memory locations each of size one byte
- Each memory location inside the allocated memory block is initialized with some garbage value
- On success of memory allocation; It returns beginning address of the allocated memory block
- if the allocation fails it returns NULL value

Syntax or prototype of malloc() function is:
(void *) malloc(size_t sizeInBytes)

where :
void * : indicates a generic pointer type which can be converted to any specific pointer type
size_t : indicates unsigned integer value i.e +ve integer value
sizeInBytes : indicates size of the memory block to be allocated in bytes

Ex 1: malloc(sizeof(int)); OR malloc(1 * sizeof(int));
- It allocates a single memory block of size 4 bytes, initializes with garbage integer values and
- returns beginning address of the allocated memory block as generic pointer type

Ex 2: malloc(5 * sizeof(int));
- It allocates a single memory block of size 20 bytes, initializes with garbage integer values and
- returns beginning address of the allocated memory block as generic pointer type

Three important parts of the memory:
There are 3 important parts or pools of the memory; which are being used by any C program. They are static memory, stack memory and heap memory.

Static memory:
- Static memory is used to allocate memory locations for global and static variables
- Variables which are declared outside a function are considered as global variables
- Variables which are declared using static keyword are considered as static variables
- Global and static variables are persisted throughout the execution of the program

Stack memory:
- Stack memory is used to allocate memory locations for local variables
- Variables which are declared inside a function are considered as local variables
- Local variables of a function are persisted as long as the function is executing

Heap memory:
- Heap memory is used to allocate memory locations; which are being allocated during run time.
- Memory locations which are allocated during run time i.e. using malloc() and calloc() functions are persisted even after the program execution ends. So it is programmer’s responsibility to free them before the program execution ends.
- If you do not free the dynamically allocated memory locations before the program execution ends then they will not be available to use for any other applications which is called as memory leak.

free() function:
- is used to de-allocate or free the dynamically allocated memory locations before the program execution ends.

Syntax or prototype of free() function is:
void free (void *pointer);
- it de-allocates or frees the memory block pointed by the given pointer

where :
void : indicates free() function does not return anything back
void *pointer : indicates any type of pointer

Ex:
free (iptr); // de-allocates or frees the memory block pointed by iptr
iptr = NULL; // makes iptr to not to point any where

Example Code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *iptr = NULL;

iptr = (int *) malloc(5 * sizeof(int));

*iptr = 10; // *#20 = 10
*(iptr + 1) = 11; // *(#24 ) = 11
*(iptr + 2) = 12; // *(#28) = 12
*(iptr + 3) = 13; // *(#32) = 13;
*(iptr + 4) = 14; // *(#36) = 14;

printf("Value of #20 = %d\n", *iptr); // 10
printf("Value of #24 = %d\n", *(iptr + 1)); // 11
printf("Value of #28 = %d\n", *(iptr + 2)); // 12
printf("Value of #32 = %d\n", *(iptr + 3)); // 13
printf("Value of #36 = %d\n", *(iptr + 4)); // 14

free(iptr);
iptr= NULL;

return 0;
}