0% found this document useful (0 votes)
2 views25 pages

Module 2 Dynamic Memory Allocation

The document provides an overview of dynamic memory allocation in C, detailing functions such as malloc(), calloc(), and realloc() for allocating memory during program execution. It includes syntax examples and usage scenarios for both 1D and 2D dynamic arrays, emphasizing the differences between static and dynamic memory allocation. Additionally, it covers memory deallocation practices to prevent memory leaks.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views25 pages

Module 2 Dynamic Memory Allocation

The document provides an overview of dynamic memory allocation in C, detailing functions such as malloc(), calloc(), and realloc() for allocating memory during program execution. It includes syntax examples and usage scenarios for both 1D and 2D dynamic arrays, emphasizing the differences between static and dynamic memory allocation. Additionally, it covers memory deallocation practices to prevent memory leaks.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

DYNAMIC MEMORY ALLOCATION

STATIC VS DYNAMIC MEMORY ALLOCATION


DYNAMIC MEMORY ALLOCATION

<stdlib.h>
DYNAMIC MEMORY ALLOCATION –malloc()
malloc()memory allocation

• malloc() is a function in C that dynamically allocates a block of


memory of a specified size (in bytes) in the heap section of memory
during the runtime of a C program.

Syntax: ptr = (int *)malloc(n * sizeof(int))

•malloc() returns a pointer to the first byte of the allocated memory


block. If the memory allocation fails, it returns a NULL pointer.
DYNAMIC MEMORY ALLOCATION-malloc()
malloc()

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

• The calloc() function in C is used to dynamically allocate a contiguous


block of memory in the heap section.
• Unlike malloc(), calloc() is used to allocate multiple blocks of memory,
typically to store an array of elements.

Syntax

(cast-type*) calloc(n, size);


•cast-type is the type you want to cast the allocated memory to (for
example, int* or float*)
•n is the number of blocks to be allocated
•size is the size of each block in bytes
DYNAMIC MEMORY ALLOCATION-calloc()
calloc()

#include < stdio.h > // Initializing the allocated memory


#include < stdlib.h > for (int i = 0; i < 5; i++)
int main() *(ptr + i) = i + 1;
{
int *ptr; // Printing the elements of the array
// Allocating memory for 5 integers printf("The elements of the array are: ");
ptr = (int*)calloc(5, sizeof(int)); for (int i = 0; i < 5; i++)
if (ptr == NULL) { printf("%d, ", *(ptr + i));
printf("Memory not allocated.\n"); }
exit(0);
} return 0;
else { }
printf("Memory successfully allocated using
calloc.\n");
DYNAMIC MEMORY ALLOCATION-realloc()
realloc()

• realloc() that allows you to change the size of a previously allocated


memory block.
• This function can be used to increase or decrease the size of a block of
memory that was previously allocated using
either malloc() or calloc(). It can also be used to allocate or de-
allocate a memory block on its own completely.

Syntax void *realloc(void *ptr, size_t


size);

•ptr is a pointer to the memory block previously allocated using malloc()


or calloc().
DYNAMIC MEMORY ALLOCATION-realloc()
realloc()
#include<stdio.h>
#include<stdlib.h>

int main()
{
int *ptr;

//allocating memory for 10 integers


ptr =(int*) malloc(10 * sizeof(int));

//realloc memory size to store only 5 integers


ptr = (int*)realloc(ptr, 5 * sizeof(int));

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

You might also like