0% found this document useful (0 votes)
18 views3 pages

Unit - 9

PPS UNIT 9

Uploaded by

try.vishal0602
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)
18 views3 pages

Unit - 9

PPS UNIT 9

Uploaded by

try.vishal0602
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/ 3

PPS – 3110003 Prepared By: Prof.

Jigar Dalvadi

UNIT-9 - Dynamic memory allocation


Introduction to Dynamic memory allocation
 When the memory is allocated during compile-time it is stored in the Static Memory and
it is known as Static Memory Allocation, and when the memory is allocated during run-
time it is stored in the Dynamic Memory and it is known as Dynamic Memory
Allocation.
 Dynamic Memory Allocation is manual allocation and freeing of memory according to
your programming needs.
 Dynamic Memory Allocation is a process in which we allocate or deallocate a block of
memory during the run-time of a program.
 The <stdlib.h> library has functions responsible for Dynamic Memory Management.
 Structure is created using the struct keyword, and a structure variable is created using
the struct keyword.

Static Memory Allocation Dynamic Memory Allocation


 Constant (Invariable) memory is reserved  Dynamic (Variable) memory is reserved
at compile-time of our program at run-time of our program that can be
that can't be modified. modified.
 It is used at compile-time of our program  It is used at run-time of our program and is
and is also known as compile-time also known as run-time memory
memory allocation. allocation.
 We can't allocate or deallocate a memory  We can allocate and deallocate a memory
block during run-time. block during run-time.
 Stack space is used in Static Memory  Heap space is used in Dynamic Memory
Allocation. Allocation.
 It doesn't provide reusability of memory  It provides reusability of memory while
while the program is running. So, it the program is running. So, it
is less efficient. is more efficient.

C malloc() Method
 malloc() is a method in C which is used to allocate a memory block in the heap section
of the memory of some specified size (in bytes) during the run-time of a C program.
 It is a library function present in the <stdlib.h> header file.

Syntax of malloc()
 ptr = (castType*) malloc(size);

Example
 ptr = (float*) malloc(100 * sizeof(float));

 Example 1: malloc() and free()

// C Program to dynamically allocate an int ptr

1|P ag e
PPS – 3110003 Prepared By: Prof. Jigar Dalvadi

#include <stdio.h>
#include <stdlib.h>
int main()
{
// Dynamically allocated variable, sizeof(char) = 1 byte.
char *ptr = (char * )malloc(sizeof(char));
if (ptr == NULL)
{
printf("Memory Error!\n");
}
else
{
*ptr = 'S';
printf("%c", *ptr);
}
return 0;
}

C calloc() Method
 calloc() is a method in C which is also used to allocate memory blocks in the heap
section, but it is generally used to allocate a sequence of memory blocks (contiguous
memory) like an array of elements.
 It is also present in <stdlib.h> header file.

Syntax of malloc()
 ptr = (castType*)calloc(n, size);

Example
 ptr = (float*) calloc(25, sizeof(float));

 Example 1: calloc() and free()

// C Program to dynamically allocate an array using calloc()


#include <stdio.h>
#include <stdlib.h>
int main()
{
int n;
scanf("%d", &n);
// Dynamically allocated array of size 10 using calloc()
// array elements are initialized with 0
// arr stores the base address of the memory block
char *str = (char *)calloc(n, sizeof(char));
if (str == NULL)
{
printf("Memory Error!\n");

2|P ag e
PPS – 3110003 Prepared By: Prof. Jigar Dalvadi

}
else
{
// initializing array with char variables
for (int i = 0; i < n; i++)
{
char ch;
scanf("%c", &ch);
*(str + i) = ch;
}
// printing array using pointer
for (int i = 0; i < n; i++) {
printf("%c", *(str + i));
}
}
return 0;
}

Difference between malloc() and calloc()


malloc() calloc()
1. Malloc() function will create a single 1. Calloc() function can assign multiple blocks
block of memory of size specified by of memory for a variable.
the user.
2. Malloc function contains garbage value. 2. The memory block allocated by a calloc
function is always initialized to zero.
3. Number of argument is 1. 3. Number of arguments are 2.
4. Calloc is slower than malloc. 4. Malloc is faster than calloc.
5. It is not secure as compare to calloc. 5. It is secure to use compared to malloc.
6. Time efficiency is higher than calloc(). 6. Time efficiency is lower than malloc().
7. Malloc() function returns only starting 7. Before allocating the address, Calloc()
address and does not make it zero. function returns the starting address and
make it zero.
8. It does not perform initialization of 8. It performs memory initialization.
memory.

3|P ag e

You might also like