Computer Programming
Module 5
Dynamic Memory Allocation Tutorial
Computer Programming Module 5 Dynamic Memory Allocation Tutorial 1/4
// How do you a l l o c a t e memory d y n a m i c a l l y f o r an a r r a y
o f i n t e g e r s i n C?
#i n c l u d e < s t d i o . h>
#i n c l u d e < s t d l i b . h>
i n t main ( ) {
int n, i ;
int ∗ ptr ;
p r i n t f ( ” E n t e r t h e number o f e l e m e n t s : ” ) ;
s c a n f ( ”%d” , &n ) ;
p t r = ( i n t ∗) malloc ( n ∗ s i z e o f ( i n t ) ) ;
i f ( p t r == NULL) {
p r i n t f ( ” E r r o r ! memory n o t a l l o c a t e d . ” ) ;
e x i t (0) ;
}
p r i n t f ( ” Enter the elements of the array : ” ) ;
Computer Programming Module 5 Dynamic Memory Allocation Tutorial 1/4
f o r ( i = 0 ; i < n ; ++i ) {
s c a n f ( ”%d” , p t r + i ) ;
}
p r i n t f ( ”The e l e m e n t s o f t h e a r r a y a r e : ” ) ;
f o r ( i = 0 ; i < n ; ++i ) {
p r i n t f ( ”%d ” , ∗ ( p t r + i ) ) ;
}
free ( ptr ) ;
return 0;
}
// How do you r e a l l o c a t e memory f o r an a r r a y o f
i n t e g e r s i n C?
#i n c l u d e < s t d i o . h>
#i n c l u d e < s t d l i b . h>
i n t main ( ) {
int n, i ;
int ∗ ptr ;
Computer Programming Module 5 Dynamic Memory Allocation Tutorial 1/4
p r i n t f ( ” E n t e r t h e number o f e l e m e n t s : ” ) ;
s c a n f ( ”%d” , &n ) ;
p t r = ( i n t ∗) malloc ( n ∗ s i z e o f ( i n t ) ) ;
i f ( p t r == NULL) {
p r i n t f ( ” E r r o r ! memory n o t a l l o c a t e d . ” ) ;
e x i t (0) ;
}
p r i n t f ( ” Enter the elements of the array : ” ) ;
f o r ( i = 0 ; i < n ; ++i ) {
s c a n f ( ”%d” , p t r + i ) ;
}
p r i n t f ( ”The e l e m e n t s o f t h e a r r a y a r e : ” ) ;
f o r ( i = 0 ; i < n ; ++i ) {
p r i n t f ( ”%d ” , ∗ ( p t r + i ) ) ;
}
Computer Programming Module 5 Dynamic Memory Allocation Tutorial 1/4
p r i n t f ( ” \ n E n t e r new s i z e o f a r r a y : ” ) ;
s c a n f ( ”%d” , &n ) ;
p t r = ( i n t ∗) r e a l l o c ( ptr , n ∗ s i z e o f ( i n t ) ) ;
p r i n t f ( ” E n t e r t h e e l e m e n t s o f t h e new a r r a y : ” ) ;
f o r ( i = 0 ; i < n ; ++i ) {
s c a n f ( ”%d” , p t r + i ) ;
}
p r i n t f ( ”The e l e m e n t s o f t h e new a r r a y a r e : ” ) ;
f o r ( i = 0 ; i < n ; ++i ) {
p r i n t f ( ”%d ” , ∗ ( p t r + i ) ) ;
}
free ( ptr ) ;
return 0;
}
// What t h i s program i s d o i n g
#i n c l u d e < s t d i o . h>
#i n c l u d e < s t d l i b . h>
Computer Programming Module 5 Dynamic Memory Allocation Tutorial 1/4
i n t main ( ) {
int r , c , i , j ;
i n t ∗∗ p t r ;
p r i n t f ( ” E n t e r t h e number o f rows : ” ) ;
s c a n f ( ”%d” , &r ) ;
p r i n t f ( ” E n t e r t h e number o f c o l u m n s : ” ) ;
s c a n f ( ”%d” , &c ) ;
p t r = ( i n t ∗∗) malloc ( r ∗ s i z e o f ( i n t ∗) ) ;
f o r ( i =0; i <r ; i ++)
p t r [ i ] = ( i n t ∗) malloc ( c ∗ s i z e o f ( i n t ) ) ;
f o r ( i = 0 ; i < r ; i ++) {
f o r ( j = 0 ; j < c ; j ++) {
ptr [ i ] [ j ] = i + j ;
}
}
p r i n t f ( ”The e l e m e n t s o f t h e 2D a r r a y a r e : \n” ) ;
Computer Programming Module 5 Dynamic Memory Allocation Tutorial 1/4
f o r ( i = 0 ; i < r ; i ++) {
f o r ( j = 0 ; j < c ; j ++) {
p r i n t f ( ”%d ” , p t r [ i ] [ j ] ) ;
}
p r i n t f ( ”\n” ) ;
}
f o r ( i = 0 ; i < r ; i ++) {
free ( ptr [ i ]) ;
}
free ( ptr ) ;
return 0;
}
Computer Programming Module 5 Dynamic Memory Allocation Tutorial 2/4
Explanation of above code
In this program, first, we prompted the user to enter the number
of rows and columns for the 2D array.
Then we used the malloc function to dynamically allocate
memory for the number of rows and columns specified by the
user.
We stored some values in the 2D array using a nested loop and
printed the elements of the array using another nested loop.
Finally, we freed the allocated memory using the free function.
Computer Programming Module 5 Dynamic Memory Allocation Tutorial 2/4
// How do you a l l o c a t e memory d y n a m i c a l l y f o r a s t r u c t
i n C?
#i n c l u d e <s t d i o . h>
struct student {
int id ;
c h a r name [ 2 0 ] ;
f l o a t marks ;
};
i n t main ( ) {
struct student ∗ ptr ;
int n, i ;
p r i n t f ( ” E n t e r t h e number o f s t u d e n t s : ” ) ;
s c a n f ( ”%d” , &n ) ;
p t r = ( s t r u c t s t u d e n t ∗) malloc ( n ∗ s i z e o f ( s t r u c t
student ) ) ;
i f ( p t r == NULL) {
p r i n t f ( ” E r r o r ! memory n o t a l l o c a t e d . ” ) ;
Computer Programming Module 5 Dynamic Memory Allocation Tutorial 2/4
e x i t (0) ;
}
p r i n t f ( ” E n t e r t h e d e t a i l s o f s t u d e n t s : \ n” ) ;
f o r ( i = 0 ; i < n ; ++i ) {
p r i n t f ( ” S t u d e n t %d\n” , i +1) ;
p r i n t f ( ” ID : ” ) ;
s c a n f ( ”%d” , &( p t r+i )−>i d ) ;
p r i n t f ( ”Name : ” ) ;
s c a n f ( ”%s ” , ( p t r+i )−>name ) ;
p r i n t f ( ” Marks : ” ) ;
s c a n f ( ”%f ” , &( p t r+i )−>marks ) ;
}
p r i n t f ( ” D i s p l a y i n g t h e d e t a i l s o f s t u d e n t s : \ n” ) ;
f o r ( i = 0 ; i < n ; ++i ) {
p r i n t f ( ” S t u d e n t %d\n” , i +1) ;
p r i n t f ( ” ID : %d\n” , ( p t r+i )−>i d ) ;
p r i n t f ( ”Name : %s \n” , ( p t r+i )−>name ) ;
p r i n t f ( ” Marks : %f \n” , ( p t r+i )−>marks ) ;
}
Computer Programming Module 5 Dynamic Memory Allocation Tutorial 2/4
free ( ptr ) ;
return 0;
}
Output :
E n t e r t h e number o f s t u d e n t s : 2
Enter the d e t a i l s of students :
Student 1
ID : 100 Name : S a i Marks : 90
Student 2
ID : 101 Name : P r i t a m Marks : 89
D i sp l a y in g the d e t a i l s of students :
Student 1
ID : 100
Name : S a i
Marks : 9 0 . 0 0 0 0 0 0
Student 2
ID : 101
Name : P r i t a m
Marks : 8 9 . 0 0 0 0 0 0
Computer Programming Module 5 Dynamic Memory Allocation Tutorial 2/4
// How do you a l l o c a t e memory d y n a m i c a l l y f o r a s t r i n g
i n C?
#i n c l u d e < s t d i o . h>
#i n c l u d e < s t d l i b . h>
#i n c l u d e < s t r i n g . h>
i n t main ( ) {
char ∗ s t r ;
int n;
p r i n t f ( ” E n t e r t h e number o f c h a r a c t e r s : ” ) ;
s c a n f ( ”%d” , &n ) ;
s t r = ( c h a r ∗ ) m a l l o c ( ( n+1) ∗ s i z e o f ( c h a r ) ) ;
i f ( s t r == NULL) {
p r i n t f ( ” E r r o r ! memory n o t a l l o c a t e d . ” ) ;
e x i t (0) ;
}
p r i n t f ( ” Enter the s t r i n g : ” ) ;
Computer Programming Module 5 Dynamic Memory Allocation Tutorial 2/4
s c a n f ( ”%s ” , s t r ) ;
p r i n t f ( ”The e n t e r e d s t r i n g i s : %s \n” , s t r ) ;
free ( str ) ;
return 0;
}
// program t h a t prompts t h e u s e r t o e n t e r a m a t r i x o f
i n t e g e r s and t h e n d y n a m i c a l l y a l l o c a t e s memory f o r
t h e m a t r i x . The program t h e n t r a n s p o s e s t h e m a t r i x
and p r i n t s i t :
#i n c l u d e < s t d i o . h>
#i n c l u d e < s t d l i b . h>
i n t main ( ) {
i n t rows , c o l s ;
p r i n t f ( ” E n t e r t h e number o f rows : ” ) ;
s c a n f ( ”%d” , &rows ) ;
p r i n t f ( ” E n t e r t h e number o f c o l u m n s : ” ) ;
s c a n f ( ”%d” , &c o l s ) ;
Computer Programming Module 5 Dynamic Memory Allocation Tutorial 2/4
i n t ∗∗ m a t r i x = ( i n t ∗ ∗ ) m a l l o c ( rows ∗ s i z e o f ( i n t ∗ ) ) ;
f o r ( i n t i = 0 ; i < rows ; i ++) {
matrix [ i ] = ( i n t ∗) malloc ( c o l s ∗ s i z e o f ( i n t ) ) ;
}
p r i n t f ( ” E n t e r t h e e l e m e n t s o f t h e m a t r i x : \ n” ) ;
f o r ( i n t i = 0 ; i < rows ; i ++) {
f o r ( i n t j = 0 ; j < c o l s ; j ++) {
s c a n f ( ”%d” , &m a t r i x [ i ] [ j ] ) ;
}
}
p r i n t f ( ” O r i g i n a l M a t r i x : \ n” ) ;
f o r ( i n t i = 0 ; i < rows ; i ++) {
f o r ( i n t j = 0 ; j < c o l s ; j ++) {
p r i n t f ( ”%d ” , m a t r i x [ i ] [ j ] ) ;
}
p r i n t f ( ”\n” ) ;
}
Computer Programming Module 5 Dynamic Memory Allocation Tutorial 2/4
p r i n t f ( ” T r a n s p o s e d M a t r i x : \ n” ) ;
f o r ( i n t i = 0 ; i < c o l s ; i ++) {
f o r ( i n t j = 0 ; j < rows ; j ++) {
p r i n t f ( ”%d ” , m a t r i x [ j ] [ i ] ) ;
}
p r i n t f ( ”\n” ) ;
}
f o r ( i n t i = 0 ; i < rows ; i ++) {
f r e e ( matrix [ i ] ) ;
}
f r e e ( matrix ) ;
return 0;
}
Computer Programming Module 5 Dynamic Memory Allocation Tutorial 3/4
Examples of dynamic memory allocation in
real-world scenarios:
Dynamic memory allocation is commonly used in image and
video processing applications to store and manipulate large
amounts of data. For example, a program that processes
high-resolution images may use dynamic memory allocation to
store the image data in memory.
In a text editor or word processing application, dynamic memory
allocation is used to store the text as it is being typed by the
user. As the user types more text, the program can use dynamic
memory allocation to increase the size of the memory buffer
holding the text.
In a database management system, dynamic memory allocation
is used to store and retrieve data from the database. It is used
to increase or decrease the amount of memory to store the data
as the amount of data in the database changes.
Computer Programming Module 5 Dynamic Memory Allocation Tutorial 3/4
Examples of dynamic memory allocation in
real-world scenarios:
In a game, dynamic memory allocation is used to store game
objects like player characters, enemies, weapons, and levels. As
the game progresses, the program can use dynamic memory
allocation to add or remove game objects from memory as
needed.
In an operating system, dynamic memory allocation is used for
memory management and to allocate memory to different
processes and applications running on the system. The operating
system can use dynamic memory allocation to increase or
decrease the amount of memory allocated to a process as its
memory usage changes.
Computer Programming Module 5 Dynamic Memory Allocation Tutorial 4/4