Lab Manual-11
Lab Manual-11
The value of the pointer variable ptr is a memory address. A data item whose address is stored in this variable
must be of the specified type.
var ptr
6024 6000
ptr = &var;
var ptr
• A limited set of arithmetic operations may be performed on pointers. A pointer may be incremented
(++) or decremented (--), an integer may be added to a pointer (+ or +=), an integer may be subtracted
from a pointer (- or -=) and one pointer may be subtracted from another.
• When an integer is added to or subtracted from a pointer, the pointer is incremented or
decremented by that integer times the size of the object to which the pointer refers.
• Two pointers to elements of the same array may be subtracted from one another to determine the
number of elements between them.
Function Syntax
malloc () malloc (number *sizeof(int));
calloc () calloc (number, sizeof(int));
realloc () realloc (pointer_name, number * sizeof(int));
free () free (pointer_name);
Malloc()
malloc function is used to allocate space in memory during the execution of the program.
malloc does not initialize the memory allocated during execution. It carries garbage value.
Malloc function returns null pointer if it couldn’t able to allocate requested amount of memory.
Calloc()
calloc function is also like malloc function. But calloc initializes the allocated memory to zero.
But, malloc doesn’t.
Realloc()
realloc function modifies the allocated memory size by malloc and calloc functions to new size.
If enough space doesn’t exist in memory of current block to extend, new block is allocated for
the full size of reallocation, then copies the existing data to new block and then frees the old
block.
Free()
free function frees the allocated memory by malloc, calloc, realloc functions and returns the
memory to the system.
Malloc & free Sample Code:
Calloc & free Sample Code:
Realloc Sample Code:
3.2 Difference between static memory allocation and dynamic memory allocation in C