Module 2 Dynamic Memory Allocation
Module 2 Dynamic Memory Allocation
<stdlib.h>
DYNAMIC MEMORY ALLOCATION –malloc()
malloc()memory allocation
int main() {
int n, i; printf("The elements you entered are: ");
int *ptr; for (i = 0; i < n; i++)
printf("Enter the number of elements: "); {
scanf("%d", &n); printf("%d ", ptr[i]);
// Allocate memory for 'n' elements using malloc() }
// Deallocate the memory using free()
ptr = (int *)malloc(n * sizeof(int)); free(ptr);
if (ptr == NULL) return 0;
{ }
printf("Memory allocation failed.\n");
exit(1);
}
printf("Enter the elements: ");
for (i = 0; i < n; i++)
{
scanf("%d", &ptr[i]);
}
DYNAMIC MEMORY ALLOCATION-calloc()
calloc() contiguous allocation
Syntax
int main()
{
int *ptr;
return 0;
}
1D ARRAY DECLARATION(DYNAMIC)
type *arrayname;
arrayname=(type*)malloc(size*sizeof(type));
Example
int *a, n=5;
a=(int*)malloc(n*sizeof(int));
DYNAMIC ARRAY MEMORY ALLOCATION 1300
1301
2
For 32 bit compiler n 1302
1303
int *a,n=2;
1304
a=(int*)malloc(n*sizeof(int));
1305
a[0]=5; 1306
a[1]=2; a[0]
1307
5
1308
1309
a[1] 1310
a=(int*)malloc(2*4)
a=(int*)malloc(8) 1311
2
a=(int*)1306 1312
1313
1314
a[1]=a+1*sizeof(int) 1315
=1306+1*4 1316
=1310 a 1317
1306
1318
READ AND DISPLAY VALUES USING DYNAMIC ARRAY
#include <stdio.h>
int main()
{
int *a,i,n;
printf("Enter a integer: ");
scanf("%d", &n);
a=(int*)malloc(sizeof(n*sizeof(int)));
for(i = 0; i<n; i++)
scanf("%d",a+i);
printf(“\nDisplay Array:\n”);
for(i = 0; i < n; i++)
printf("%d\t",*(a+i));
return 0;
}
2 D ARRAYS
• an array of arrays.
• organized as matrices which can be represented as the collection of rows and
columns.
2D DYNAMIC ARRAY DECLARATION(3 methods)
Using single pointer
int *a;
Single pointer → Best for contiguous
a=(int*)malloc(r*c*sizeof(int)); memory and faster access.
Array of pointers → Useful when rows need
Using Array of pointers independent memory allocation.
Double pointer → Suitable when both rows
int* a[r]; and columns are fully dynamic at runtime.
for (i = 0; i < r; i++)
a[i] = (int*)malloc(c * sizeof(int));
Double pointers
int **a=(int**)malloc(r*sizeof(int*));
for(i=0;i<r;i++)
a[i]=(int*)malloc(c*sizeof(int));
2D DYNAMIC ARRAY DECLARATION(using double pointer)
Using double pointer
2D DYNAMIC ARRAY EXAMPLE(SINGLE POINTER)
#include<stdio.h> printf("Matrix A:\n");
#include<stdlib.h> for(i=0;i<r;i++)
int main() {
{ for(j=0;j<c;j++)
int *a,i,j,r,c; {
printf("Enter row and column:"); printf("%d\t",*(a+i)
scanf("%d%d",&r,&c); +j); }
a=(int*)malloc(r*c*sizeof(int)); printf("\n");
printf("Enter the values:"); }
for(i=0;i<r;i++) free(a);
{ }
for(j=0;j<c;j++)
{
scanf("%d",(a+i)+j);
}
}
2D DYNAMIC ARRAY(Double Pointer) EXAMPLE
#include<stdio.h> printf("Matrix A:\n");
#include<stdlib.h> for(i=0;i<r;i++)
int main() {
{ for(j=0;j<c;j++)
int **a,i,j,r,c; {
printf("Enter row and column:"); printf("%d\
scanf("%d%d",&r,&c); t",*(*(a+i)+j));
a=(int**)malloc(r*sizeof(int*)); }
for(i=0;i<r;i++) printf("\n");
a[i]=(int*)malloc(c*sizeof(int)); }
printf("Enter the values:"); for(i=0;i<r;i++)
or
for(i=0;i<r;i++) free(a[i]);
{ free(a);
for(j=0;j<c;j++) return 0;
{ } printf("%d”,a[i][j]);
scanf("%d",*(a+i)+j); or scanf("%d”,&a[i][j]);
} }
2D ARRAY DYNAMIC MEMORY ALLOCATION
2D ARRAY DYNAMIC MEMORY ALLOCATION
2D ARRAY DYNAMIC MEMORY ALLOCATION