0% found this document useful (0 votes)
40 views

Dynamic Memory Allocation

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

Dynamic Memory Allocation

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Winter Semester 2023-2024

Structured and Object Oriented Programming


Course Code: BCSE102L
SHREE LAKSHMI. K
23BCE2120

Dynamic memory
What is Dynamic Memory?
It is the procedure through which the size of a Data structure (like Array) is
changed during the run time.
For example:
If the maximum size of an array is 9, and the entered array is of size 5 then in
this case the remaining 4 indices are just wasting memory to save this memory
we can use dynamic memory during the run time of the code to lessen the size
of the array from 9 to 5.
If the entered array is greater than 9 for example if the size of the entered array
is 12 then we require 3 more indices so the length of the array can be changed
from 9 to 12.
This procedure is known as Dynamic Memory allocation.
There are four predefined library functions to help run dynamic memory
allocation.
The file under which the functions are present is <stdlib.h>.
The four functions are:
1.malloc( )
2.calloc( )
4.realloc( )
3.free( )

1.malloc()
malloc() function is used to allocate single block of requested
memory dynamically. It doesn’t initialize memory at execution time,
so it has garbage value initially. It returns NULL if the requested
memory is not sufficient. If the memory is sufficient it return the
address of the memory in which the data is stored
Syntax:
int n,*ptr;
ptr=(int*)malloc(n*size of(int))
int* the type of data that is stored in the heap memory.
n*size of(int) allocates the memory size.
Ptr stores the base address of the memory in which the data is stored.
2.calloc()
Calloc full form is contagious allocation. Built in function in stdlib.h
library. Used to dynamically allocate multiple blocks of memory and
each block is of the same size.
Malloc allocates single block of memory calloc allocates multiple
blocks of memory

Calloc accepts 2 arguments while malloc accepts only one argument

Syntax for calloc function:


Void* Calloc(size_t n,size_t size)

(Malloc only takes size,


Size_t only takes 0 or positive values.
n means the number of blocks or the number of elements you want the
calloc to allocate memory from the heap, size gives the size of each
blocks.)

Ex: Calloc (5,size of(int));


This means we need 5 blocks of integer type
If calloc run successfully then calloc returns the base address of the
first block.
It returns the address in the form of void pointer.
Ex:
If u want to store integer values in the block, then we type cast the
syntax to integer
(Int*) calloc (n, size of(size));
We store the address in a ptr, hence we initialise the pointer initially.

Int *ptr; //static memory allocation//


Ptr=(int*) calloc(5,size of(size));//dynamic memory allocation//
Ptr now stores the first base address of it allocates the memory
successfully,

If the size of each block is 2 bytes say,


Calloc allocates 5 blocks of each 2 bytes
Malloc allocates 1 block of 10 bytes

In calloc the memory value is initialised with 0 automatically.


In malloc the memory value is initialised with a garbage value.
If we want the initial memory value to be 0 then only we should use
calloc to allocate memory dynamically.
3.realloc()
Full form is Reallocation,
Resize the memory (increase or decrease the size of the memory)
Can be used only when malloc or calloc function is used to allocate
memory in the programme.
If used in a static memory, then it shows undefined behaviour.
Built in function declared in stdlib.h.
Accepts 2 arguments, previously allocated pointer and the new size
we want to resize it with.
Syntax:
(Void*)Realloc(void*ptr,size_t size);
The most important feature of realloc is that it resizes the memory
block without losing the previous content.
Ex;
Int *ptr;
Ptr=(Int*) malloc(5*sizeof(int));
Ptr1=(int*)realloc(ptr,7*sizeof(int));
In heap section suppose we store five integers and let the first byte
address be 1000, then ptr returns 1000, suppose now we want to add 2
more blocks of memory then we do the following. The previous
content will be as it is and the first byte address also remains the same
if the same block is expanded.
Sometimes when the block is not expandable then the realloc will
create a new block to store the memory. It copies the memory from
the previous block to the new block (resized block) and then the first
byte address changes and the address is stored in ptr1.
The content will remain the same the address may change but the
content will always remain the same.
If on failure of the reallocation it returns null.
If the size is reduced during reallocation, then data would be lost.
4.free():
Releases the dynamically allocated memory.
Built in function defined in stdlib.h
Syntax:
Void Free(pointer)
Ex:
Int *ptr;
Ptr=(int*)malloc(2*sizeof(int));
Free(ptr);
Data is from heap section.
Suppose base address is 1000 which is stored in ptr.
Once we free ptr, the memory is freed and the memory goes back to
heap section.
But still ptr has 1000(dangling pointer) and this memory can be
reused after free we can use malloc or calloc then the freed memory
can be reused.
The free function doesn’t erase the previous data.
After freeing the memory, the data cannot be accessed but the data is
not erased. Basically it shows undefined behaviour. After freeing it’s
not safe to use the same pointer.
If we use static memory allocation to allocate values, the data is lost
once we exit the program.
In dynamic memory the memory is not released automatically that is
why we use free() and at some point of time the memory will be
exhausted and cause the program to crash.
Dangling pointer: it is pointing towards a known existing memory
location.
After using free it is always better to reinitialize the pointer.
(ptr=NULL);
We should always use free() after using malloc calloc realloc to
protect our program from crashing.

You might also like