C Programming Dynamic Memory Allocation
C Programming Dynamic Memory Allocation
& %
Lect 23 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 2
void *malloc(size t n)
& %
Lect 23 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 4
void *malloc(size t n)
& %
Lect 23 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 5
Note
& %
Lect 23 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 6
& %
Lect 23 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 7
& %
Lect 23 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 8
& %
Lect 23 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 9
& %
Lect 23 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 10
C Program
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *p, (*q)[5], *r[3], **s;
& %
putchar(’\n’);
Lect 23 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 11
& %
Lect 23 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 12
Output
$ cc -Wall pointVar.c
$ ./a.out
sizeof(int): 4
sizeof(int *): 4
sizeof(int [5]): 20
sizeof(int (*)[5]): 4
sizeof(int **): 4
& %
Lect 23 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 13
& %
Lect 23 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 14
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *p, n, i, val = 1 ;
& %
}
Lect 23 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 15
& %
Lect 23 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 16
p[0] p[9]
p
in the Dynamically Allocated Memory
stack−frame Globally accessible space
of main()
& %
Lect 23 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 17
Output
$ cc -Wall dynamic1D.c
$ ./a.out
Enter a +ve integer: 10
1 2 4 8 16 32 64 128 256 512
& %
Lect 23 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 18
The code
p = (int *) malloc(n*sizeof(int));
allocates a contiguous memory area for n
locations of type int. It returns the starting
address as a void * pointer. The pointer is
casted to the type int * and is assigned to the
variable p.
& %
Lect 23 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 19
& %
Lect 23 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 20
... what(...) {
int a[10], *p ;
p = (int *) malloc(10*sizeof(int)) ;
p[0] p[9]
p
Static Alloc
in Stack Frame Dynamic Alloc in Global Data Area
a[0] a[9]
a
& %
Lect 23 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 21
#include <stdio.h>
#include <stdlib.h>
int g[5]; // global
int main()
{
int a[5], *p, n ;
static int s[5] ;
scanf("%d", &n);
int b[n];
p=(int *)malloc(20) ;
& %
Lect 23 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 22
& %
Lect 23 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 23
Output
$ cc -Wall memoryAlloc.c
$ ./a.out
$ 100
g: 0x804a03c s: 0x804a028 p:
0x8267008 a: 0xbfc4b268 b: 0xbfc4b0a0
& %
Lect 23 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 24
Note
& %
Lect 23 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 25
& %
Lect 23 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 26
#include <stdio.h>
#include <stdlib.h>
int main()
{
int (*q)[5],rows,i,j;
}
return 0;
} // dynamic2D1.c
& %
Lect 23 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 28
Output
$ cc -Wall dynamic2D1.c
$ ./a.out
Enter the number of Rows: 3
0 3 6 9 12
2 5 8 11 14
4 7 10 13 16
& %
Lect 23 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 29
... what(...) {
int (*q)[5] ;
q=(int (*)[5])malloc(rows*5*sizeof(int)) ;
q
Dynamically Allocated Memory
& %
Lect 23 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 30
n × 5 2-D Array
& %
Lect 23 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 31
n × 5 2-D Array
& %
Lect 23 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 32
Note
& %
Lect 23 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 33
#include <stdio.h>
void vla(int r, int c, int a[r][c]);
int main() // varLenArray.c
{
int row, col;
printf("Enter row and column numbers: ");
scanf("%d%d", &row, &col);
int x[row][col], i, j;
for(i=0; i<row; ++i)
& %
Lect 23 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 34
& %
Lect 23 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 36
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *r[3], i, j;
for(i=0; i<3; ++i) {
int col = 2*(i+1) ;
r[i] = (int *) malloc(col*sizeof(int)) ;
for(j=0; j<col; ++j) r[i][j] = i+j ;
}
for(i=0; i<3; ++i) {
int col = 2*(i+1) ;
for(j=0; j<col; ++j)
printf("%d ", r[i][j]) ; printf("\n");
& %
Lect 23 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 37
}
return 0;
} // dynamic2D2.c
& %
Lect 23 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 38
Output
$ cc -Wall dynamic2D2.c
$ ./a.out
0 1
1 2 3 4
2 3 4 5 6 7
& %
Lect 23 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 39
int *r[3] ;
r
r[0]
r[2]
Static Alloc Dynamically Allocated Memory
1−D Array
of Pointers
& %
Lect 23 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 40
Note
& %
Lect 23 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 41
& %
Lect 23 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 42
#include <stdio.h>
#include <stdlib.h>
int main()
{
int **s, row, column, i, j;
& %
Lect 23 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 43
& %
Lect 23 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 44
Output
$ cc -Wall dynamic2D3.c
$ ./a.out
Enter Row & Column:
3 5
0 1 2 3 4
1 2 3 4 5
2 3 4 5 6
& %
Lect 23 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 45
int **s ;
s Static Allocation
s[0]
s[2]
& %
Lect 23 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 46
Note
& %
Lect 23 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 47
& %
Lect 23 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 48
& %
Lect 23 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 49
& %
Lect 23 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 50
#include <stdio.h>
#include <stdlib.h>
int *ret1DArray(int);
int main()
{
int n, *p, i ;
& %
for(i=0; i<n; ++i) printf("%d ", p[i]);
Lect 23 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 51
putchar(’\n’);
return 0;
}
int *ret1DArray(int n){
int *p = (int *)malloc(n*sizeof(int)), i;
& %
Lect 23 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 52
Use of realloc()
#include <stdio.h>
#include <stdlib.h>
int *ret1DArray(int *);
int main()
{
int n, *p, i ;
putchar(’\n’);
return 0;
}
#define MAX 5
int *ret1DArray(int *nP){
int n, max = MAX,
*p = (int *)malloc(MAX*sizeof(int));
}
*nP = n ;
return p;
} // retDynArray2.c
& %
Lect 23 Goutam Biswas