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

Dynamic Memory Allocation

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views

Dynamic Memory Allocation

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Dynamic Memory Allocation

Problem
• As you know, an array is a collection of a fixed number of
values. Once the size of an array is declared, you cannot change
it.
int A[5];
[0] [1] [2] [3] [4]
A 10 20 30 40 50

• Sometimes the size of the array you declared may be


insufficient.
• To solve this issue, you can allocate memory manually during
run-time.
Dynamic Memory Allocation
• The process of allocating memory at runtime is called Dynamic
Memory Allocation.
• Mainly there are four functions for dynamic memory management
defined under <stdlib.h> , namely:
– malloc()
– calloc() Allocating memory
– realloc()
– free() De-allocating memory

• The malloc(), calloc() and realloc() function


reserves of memory of the specified number of bytes. And, it
returns a pointer of void which can be casted into pointers of
any type.
malloc()
• malloc()allocates memory from a freely available memory and
returns a pointer to a block of contiguous memory of specified
size.
• It initializes each block with default garbage value.
• Syntax:
ptr_name =(data_type *) malloc (byte_size);
Example:
int *a;
a=(int *)malloc(8);

24 8 bytes 256 35
1010 1012 1020 1022 1024 1026

a 1012
Program: array of elements are stored in dynamically allocated
#include <stdio.h> memory using malloc()
#include <stdlib.h>
void main()
{
int *ptr, n, i;
printf(“enter the number of elements\n”);
scanf(“%d”,&n);
ptr=(int *) malloc(n*sizeof(int));
printf(“Enter %d elements\n”,n);
for(i=0;i<n;i++)
{
scanf(“%d”,ptr);
ptr++;
}
printf(“contents of array are\n”);
for(i=0;i<n;i++)
{
printf(“%d\t”,*ptr);
ptr++;
}
}
calloc()
• calloc()allocates memory for array elements and returns a
pointer to memory. (i.e) allocates multiple blocks of memory of
specified size. It initializes each block with a default value ‘0’.
• Syntax:
ptr_name =(data_type *) calloc (n, size);
where, n is number of memory blocks
size is size of each block in bytes
Example:
int *a;
a= (int *)calloc(4,2);
0 0 0 0
1010 1012 1014 1016 1018 1020 1022 1024 1026

a 1012
Program: array of elements are stored in dynamically allocated
#include <stdio.h> memory using calloc()
#include <stdlib.h>
void main()
{
int *ptr, n, i;
printf(“enter the number of elements\n”);
scanf(“%d”,&n);
ptr=(int *) calloc(n, sizeof(int));
printf(“Enter %d elements\n”,n);
for(i=0;i<n;i++)
{
scanf(“%d”,&ptr[i]);
}
printf(“contents of array are\n”);
for(i=0;i<n;i++)
{
printf(“%d\t”,ptr[i]);
}
}
realloc()
• realloc() is used to dynamically change the memory allocation
of a previously allocated memory.

• In other words, if the memory previously allocated with the help of


malloc or calloc is insufficient, realloc can be used to dynamically
re-allocate memory.

• re-allocation of memory maintains the already present value and


new blocks will be initialized with default garbage value.
• Syntax:
ptr_name =realloc (ptr_name, new_size);
where, ptr_name is reallocated with new size 'new_size'
Example
Program: array of elements are stored in dynamically
allocated memory using realloc()
#include <stdio.h>
#include <stdlib.h>
void main()
{
int* ptr, i, n = 5;
printf("Enter number of elements: %d\n", n);
// Dynamically allocate memory using calloc()
ptr = (int*)calloc(n, sizeof(int));
for (i = 0; i < n; ++i) {
ptr[i] = i + 1;
}
printf("The elements of the array are: ");
for (i = 0; i < n; ++i) {
printf("%d, ", ptr[i]);
}
Program Cont..

// Get the new size for the array


n = 10;
printf("\n\nEnter the new size of the array: %d\n", n);

// Dynamically re-allocate memory using realloc()


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

for (i = 5; i < n; ++i) {


ptr[i] = i + 1;
}
printf("The elements of the array are: ");
for (i = 0; i < n; ++i) {
printf("%d, ", ptr[i]);
}
}
free()
• free() used to dynamically de-allocate the memory.

• The memory allocated using functions malloc() and


calloc() is not de-allocated on their own. Hence the
free() method is used, whenever the dynamic
memory allocation takes place. It helps to reduce
wastage of memory by freeing it.
• Syntax:
free(ptr_name);
Example
int *a=(int *)malloc(8);
free(a);
24 8 bytes 256 35
1010 1012 1022 1024 1026

a 1012
NULL

int * ptr= (int *)calloc(4,2);


free(ptr);

0 0 0 0
1010 1012 1014 1016 1018 1020 1022 1024 1026

a 1012
NULL
Program: De-allocating memory
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *ptr, *ptr1, n=5, i;

// Dynamically allocate memory using malloc()


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

// Dynamically allocate memory using calloc()


ptr1 = (int*)calloc(n, sizeof(int));

// Free the memory which is allocated using malloc()


free(ptr);
printf("Malloc Memory successfully freed.\n");

// Free the memory which is allocated using calloc()


free(ptr1);
printf("Calloc Memory successfully freed.\n");
}
Checking memory is allocated dynamically
• If free memory is available the memory is successfully allocated using malloc(),
calloc() and realloc() and these function returns the starting address of the memory.
• If space is insufficient, allocation fails and returns a NULL pointer.

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


ptr1 = (int*)calloc(n, sizeof(int));
if (ptr == NULL) {
printf("Memory not allocated.\n");
exit(0);
}
else {
printf("Memory successfully allocated using malloc.\n");
}
if (ptr1 == NULL) {
printf("Memory not allocated.\n");
exit(0);
}
else {
printf("Memory successfully allocated using calloc.\n");
}
Program to calculate the sum of n numbers entered by the user
#include <stdio.h>
#include <stdlib.h>

int main()
{
int n, i, *ptr, sum = 0;
printf("Enter number of elements: ");
scanf("%d", &n);

ptr = (int*) calloc(n, sizeof(int));


if(ptr == NULL)
{
printf("Error! memory not allocated.");
exit(0);
}

printf("Enter elements: ");


for(i = 0; i < n; ++i)
{
scanf("%d", ptr + i);
sum += *(ptr + i);
}

printf("Sum = %d", sum);


free(ptr);
return 0;
}

You might also like