realloc() and free() function in C

Notes:

realloc() and free() function in C Programming Language:

realloc() function : stands for re allocation
- it is used to reallocate or resize a memory block or a memory location; which is previously allocated using malloc() or calloc() function

Syntax or prototype of realloc() function is:
(void *) realloc (void * pointer, size_t newSizeInBytes);

where :
void * : indicates a generic pointer type which can be converted to any specific pointer type
void * pointer : indicates any type of pointer
newSizeInBytes : indicates new size of the reallocated space in bytes
size_t : indicates unsigned integer value i.e. +ve integer value

Note:
- If there is a sufficient amount of space under the memory block or memory location pointed by the given pointer then it resizes it to the given newSizeInBytes and returns the beginning address
- If there is no sufficient amount of space under the memory block or memory location pointed by the given pointer then it allocates a new space of the given newSizeInBytes and copies the content to it, and then returns the beginning address
- if the allocation or resizing fails it returns NULL value

Example Code:

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

int main()
{
int *iptr = NULL;

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

*iptr = 10; // *#100 = 10;

printf("Value of #100 is=%d\n", *iptr); // 10

iptr = (int *) realloc(iptr,2 * sizeof(int));
*(iptr + 1) = 20; // *(#104 ) = 20;

printf("Value of #104 is=%d\n", *(iptr+1)); // 20

free(iptr);
iptr = NULL;

return 0;
}