0% found this document useful (0 votes)
6 views7 pages

Dynamic Memory Allocation Memory Allocation

Uploaded by

rishinot8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views7 pages

Dynamic Memory Allocation Memory Allocation

Uploaded by

rishinot8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Dynamic memory allocation

Memory allocation is the procedure of assigning the computer memory for the execution of
programs and processes.
Dynamic allocation technique is used when how much of memory space is needed for the
program and process is not known.
Dynamic memory allocation arises due to the problem associated with static memory
allocation such as if fewer elements are stored, then the rest of the memory is unnecessarily
wasted. Therefore, it overcomes the problem of static memory allocation where memory is
allocated only when it is required.
The name malloc() and calloc() are library functions that allocate memory dynamically. It
means that memory is allocated during runtime i.e. during the execution of the program.
Definition of malloc()
Allocates a memory block of given size (in bytes)
Returns void type of pointer, that means any type of pointer can be assigned.
Doesn’t initialize the allocated memory. If we try to access the content of memory block
(before initializing) then we’ll get segmentation fault error (or maybe garbage values).
void* malloc(size_t size);
int *ptr;
ptr=malloc(size*sizeof (int)); (int*)malloc(5 * sizeof(int))
Here size represents the size of memory required in bytes i.e. the number of contiguous
memory location to be allocated

ptr

Address of 1st byte

Storage space allocated dynamically has no name and so its content can be accessed only
through a pointer
Definition of calloc()
The calloc function operates precisely same as malloc function excluding the fact that it
requires two arguments as in case of malloc() only one argument is needed.
1) Number of blocks to be allocated
2) Size of each block
Allocates memory and also initializes the allocated memory block to zero. If we try to access
the content of these blocks then we’ll get 0.

For example:
int*ptr;
ptr = (int*)calloc(10,2);

Here 2 specifies the size of the data type in byte for which we want the allocation to be
made, which in this case is 2 for integers and 10 signifies the number of elements for which
allocation is to be made.
A memory block of 20 bytes is allocated to the requesting program and the address of the
first block is assigned to the pointer ptr.

Return value: After successful allocation in malloc() and calloc(), a pointer to the block of
memory is returned otherwise NULL value is returned which indicates the failure of
allocation.
Program to allocate memory for array of 5 integers:-
// C program to demonstrate the use of calloc() and malloc()
#include <stdio.h>
#include <stdlib.h>
int main()
{int *arr, i;
arr = (int*)malloc(5 * sizeof(int)); // 5*4bytes = 20 bytes
for(i=0;i<5;i++)
{scanf("%d",arr); //1 2 3 4 5
printf("%d\n",*arr);} //1 2 3 4 5
free(arr); // Deallocates memory previously allocated by malloc() function
for(i=0;i<5;i++)
printf("%d\n",*arr); //0 0 0 0 0
// calloc() allocates the memory for 5 integers and sets 0 to all of them
arr = (int*)calloc(5, sizeof(int));
for(i=0;i<5;i++)
{scanf("%d",arr); //11 22 33 44 55
printf("%d\n",*arr);} //11 22 33 44 55
// Deallocates memory previously allocated by calloc() function
free(arr);
for(i=0;i<5;i++)
printf("%d\n",*arr);
return (0);
}
Key Differences Between malloc and calloc
The primary differences between malloc and calloc functions are:
1. A single block of demanded memory is assigned in malloc while multiple blocks of
requested memory are allocated by calloc.
2. The malloc function doesn’t clear and initialize the allocated memory. It contains garbage
value and item of the allocated memory. In contrast, calloc initializes the allocated memory
to zero.
3. malloc is faster than calloc due to the requirement of additional steps of initialization in
the calloc but the difference is negligible.

BASIS OF COMPARISON MALLOC( ) CALLOC( )

No of blocks Assigns single block of demanded memory. Assigns multiple blocks of the requested
memory.

Syntax void *malloc(size_t size); void *calloc(size_t num, size_t size);

Initialization malloc() doesn't clear and initialize the The allocated memory is initialized to zero
allocated memory. by using calloc().

Manner of Allocation malloc() function allocates memory of size calloc() function allocates memory the size
'size' of which is equal to num *size.

Speed Fast Comparatively slow.


#include <stdio.h>
What is the size of table?
#include <stdlib.h> 5
void main(){ Address of the first byte is 2838090432
int *p, *table; Input table values –1 2 3 4 5
int size; 1 is stored at address 2838090432
printf("What is the size of table?\n"); 2 is stored at address 2838090436
scanf("%d", &size); 3 is stored at address 2838090440
printf("\n"); 4 is stored at address 2838090444
5 is stored at address 2838090448
/*Memory allocation*/
5 is stored at address 2838090448
if ((table=(int *)malloc(size*sizeof(int)))==0) 4 is stored at address 2838090444
{printf("No space is available\n"); 3 is stored at address 2838090440
exit(1);} 2 is stored at address 2838090436
printf("Address of the first byte is %u\n", table); 1 is stored at address 2838090432
/*Reading table values*/
printf("Input table values\n");
for(p=table;p<table+size;p++)
{scanf("%d",p);
printf("%d is stored at address %u\n",*p,p);}
/*Printing table values in reverse order*/
for(p=table+size-1;p>=table;p--)
printf("%d is stored at address %u\n", *p,p);}
Allocating multiple blocks of memory
Enter elements of array
1
#include <stdio.h>
2
#include <stdlib.h>
3
int main()
4
{int *p, n=4, i;
Elements of array are
p=(int *)calloc(n, sizeof(int));
1 is stored at 3108397728
if (p==0)
2 is stored at 3108397732
printf("Memory can not be allocated\n");
3 is stored at 3108397736
else
4 is stored at 3108397740
{printf("Enter elements of array\n");
for(i=0;i<n;i++)
scanf("%d", &*(p+i)); /*value at address (p+i)*/
printf("Elements of array are\n");
for(i=0;i<n;i++)
printf("%d is stored at %u\n", *(p+i), (p+i));}
return 0;}

You might also like