❑ All the programs that we have seen so far created objects at compile time
❑ In that the compiler create and destroy objects at the appropriate times.
❑ If create objects at run time ,in that we do not inform the compiler about
that.
❑ We create an object of appropriate type over selves at run time.
❑ Since the memory is allocated for the objects “dynamically” ,creation of
such objects is known as DMA.
❑ To allocate dynamic memory we have to use some function , which is
available in alloc.h header file.
❑ Using array in programming, we allocate a fixed size for our data.
❑ This size can't be increased or decreased while execution of the program.
❑ We can't change it even if the size allocated is more or less than our
requirement.
❑ This type of allocation of memory is called Static Memory Allocation.
❑ This leads to wastage or shortage of memory.
❑ It prevents overflow and underflow of memory.
❑ DMA allows us to shrink or expand the allocated memory. Hence, there
will be no overflow or underflow.
❑ Programmer doesn't need to know about required memory space. So it
prevents unnecessary updating of program.
❑ Memory will not be wasted.
❑ Program often becomes long and complex. Using array is much ore
simpler and easier than using functions like malloc, calloc, realloc and
free.
❑ Memory fragmentation: Sometimes it may be a case that we have
sufficient memory as required but they can't be used because they are
fragmented.
❑ User is responsible for freeing up memory when finished with it.
❑ Variables get allocated only if your ❑ Variables get allocated permanently
program unit gets active ❑ Allocation is done before program
❑ Allocation is done during program execution
execution ❑ Memory size can’t be modified while
❑ Memory size can be modified while execution. Example: array
execution . Example: Linked list ❑ There is no memory reusability
❑ There is memory reusability and ❑ Less efficient
memory can be freed when not
❑ Compile time memory allocation
required
❑ More efficient
❑ Run time memory allocation
❑ The malloc function is used to allocate memory dynamically.
❑ Using malloc function block of memory can be allocated.
❑ Syntax:
Pointer_variable=(data type *) malloc(size in bytes);
❑ Malloc function takes memory of specified size and return void type pointer.
❑ This pointer can be assigned to any type of pointer.
❑ Example:
int ptr*;
ptr(int *)malloc(10*sizeof(int));
In above code the malloc function allocated memory for 10 integer elements run
time and return int type poiner.
❑ The calloc function is used to allocate multiple blocks memory dynamically and
initialize the block with zero.
❑ This function is also allocate continues memory so if enough memory is not
available then the function returns NULL value.
❑ Syntax:
Pointer_variable=(data type *) calloc (n , size of block in bytes);
❑ calloc function takes memory of specified size and return void type pointer.
❑ This pointer can be assigned to any type of pointer.
❑ Pointer is data type and n is the number of elements that want to store.
❑ Example:
int ptr*;
ptr (int *) calloc (10,sizeof(int));
In above code the calloc function allocated memory integer array of size 10 and it
will return the starting address of the array.
❑ The realloc function is used to modify the size of previously allocated space.
❑ If memory is already allocated and if we want to increase or decrease the
allocated memory space then realloc function is used.
❑ Syntax:
Pointer_variable=(data type *) malloc (size in bytes);
Will allocate memory space.
Pointer_variable=realloc (Pointer_variable , new size of memory);
Will modify allocation of memory.
❑ Example:
int ptr*;
ptr(int *)malloc(10*sizeof(int));
In above code the malloc function allocated memory for 10 integer elements run
time and return int type poiner.
ptr(int *)realloc(ptr,20*sizeof(int));
In above code the realloc function modified allocation of memory for 20integer
elements run time.
❑ The free function is used to de allocate the allocated memory space.
❑ If dynamic memory allocation memory is not free after use,it should explicitly
free afterr use with the free function.
❑ Syntax:
free(Pointer_variable);
❑ Example:
int ptr*;
ptr(int *)malloc(10*sizeof(int));
pree(ptr);