0% found this document useful (0 votes)
12 views32 pages

CTSD-2 Unit - 2

The document outlines a course on Computational Thinking for Structured Design, focusing on dynamic memory allocation in C programming. It covers key concepts such as malloc, calloc, realloc, and free functions, emphasizing their importance for efficient memory management. Additionally, it discusses the applications of C in various fields including operating systems, embedded systems, and artificial intelligence.

Uploaded by

ommahajan9890
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)
12 views32 pages

CTSD-2 Unit - 2

The document outlines a course on Computational Thinking for Structured Design, focusing on dynamic memory allocation in C programming. It covers key concepts such as malloc, calloc, realloc, and free functions, emphasizing their importance for efficient memory management. Additionally, it discusses the applications of C in various fields including operating systems, embedded systems, and artificial intelligence.

Uploaded by

ommahajan9890
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/ 32

303105151 -

Computational Thinking for


Structured Design-2
Dr. Harish Prajapati, Assistant Professor
AI & AIDS(PIET)
Scheme of the Subject
Course Outcome
After Completion of course students shall be able to :
1. Learn to use data structures concepts for realistic
Problems
2. Ability to identify appropriate data structures for Solving
computing problems in respective language.
3.Ability to solve problems independently and think
critically.
4. Understand the concept of File Management.
CHAPTER-2
Dynamic Memory Allocation:
 Introduction
 Merits of DMA
 malloc
 calloc
 realloc
 free functions
Dynamic memory allocation
 Problem with Arrays:
 The length (size) of the array is fixed. But what if there is a requirement
to change this length (size)?
 For example,
 If there is a situation where only 5 elements are needed to be entered in
array of size 9. In this case, the remaining 4 indices are just wasting
memory in this array. So there is a requirement to lessen the length
(size) of the array from 9 to 5.
 Take another situation. In this, there is an array of 9 elements with all 9
indices filled. But there is a need to enter 3 more elements in this array.
In this case, 3 indices more are required. So the length (size) of the
array needs to be changed from 9 to 12.
Dynamic memory allocation
Dynamic memory allocation
 This procedure is referred to as Dynamic Memory
Allocation in C.
 Dynamic memory allocation is essential for efficient
memory management in C.
 Therefore, C Dynamic Memory Allocation can be
defined as a procedure in which the size of a data
structure (like Array) is changed during the runtime.
Dynamic memory allocation
 There are 4 library functions provided by C defined
under <stdlib.h> header file to facilitate dynamic
memory allocation in C programming. They are:
 malloc()
 calloc()
 free()
 realloc()
Merits of DMA
1.Allocation of memory: In static memory allocation the variables are
allocated permanently hence a wastage of memory is occurred, But in case of
dynamic memory allocation the variables are get allocated until the program
unit gets inactive. Memory allocation occurs during runtime.
2.Efficiency: The efficiency of dynamic memory allocation is more than static
memory allocation and it is very flexible also.
3.Memory reusability: The previously used memory can also be reused in
dynamic memory allocation, a function named “free” is used to release the
memory for future use when the user need it.
4.Reallocation of memory: In static memory allocation after a memory is
allocated we can’t change its size. But in dynamic memory allocation using
the “realloc()” function we can change the memory block size as per our
requirement.
Dynamic memory allocation
 There are 4 library functions provided by C defined
under <stdlib.h> header file to facilitate dynamic
memory allocation in C programming. They are:
 malloc()
 calloc()
 free()
 realloc()
malloc()
 The “malloc” or “memory allocation” method in C
is used to dynamically allocate a single large block
of memory with the specified size.
 Syntax of malloc() in C
 ptr = (cast-type*) malloc(byte-size)
 For Example:
 ptr = (int*) malloc(100 * sizeof(int));
Since the size of int is 4 bytes, this statement will
allocate 400 bytes of memory. And, the pointer ptr
holds the address of the first byte in the allocated
memory.
malloc()
Example of malloc()
#include <stdio.h> // Check if the memory has been successfully
#include <stdlib.h> // allocated by malloc or not
int main() if (ptr == NULL) {
{ printf("Memory not allocated.\n");
int* ptr; exit(0);
int n, i; }
// Get the number of elements for the else {
array // Memory has been successfully allocated
printf("Enter number of elements:"); printf("Memory successfully allocated
scanf("%d",&n); using malloc.\n");
printf("Entered number of elements:
%d\n", n);
// Dynamically allocate memory using
malloc()
ptr = (int*)malloc(n * sizeof(int));
Example of malloc()
// Get the elements of the array Enter number of elements:8
for (i = 0; i < n; ++i) { Entered number of elements: 8
ptr[i] = 3*i+5; Memory successfully allocated using
} malloc.
The elements of the array are:
// Print the elements of the array 5,8,11,14,17,20,23,26
printf("The elements of the array are: ");
for (i = 0; i < n; ++i) {
printf("%d, ", ptr[i]);
}
}

return 0;
}
calloc()
“calloc” or “contiguous allocation” method in C is
used to dynamically allocate the specified number of
blocks of memory of the specified type.
it is very much similar to malloc() but has two different
points and these are:
It initializes each block with a default value ‘0’.
It has two parameters or arguments as compare to
malloc().
calloc()
Syntax of calloc() in C
ptr = (cast-type*)calloc(n, element-size);
here, n is the no. of elements and element-size is the size of
each element.

For Example:
ptr = (float*) calloc(25, sizeof(float));
This statement allocates contiguous space in memory for 25
elements each with the size of the float.
If space is insufficient, allocation fails and returns a NULL
pointer.
calloc()
calloc()
// Check if the memory has been
Example of calloc() in C successfully
#include <stdio.h> // allocated by calloc or not
#include <stdlib.h> if (ptr == NULL) {
int main() printf("Memory not
{ allocated.\n");
int* ptr; exit(0);
int n, i; }
// Get the number of elements for the else {
array
n = 5; // Memory has been
printf("Enter number of elements: %d\n", successfully allocated
n); printf("Memory successfully
// Dynamically allocate memory using allocated using calloc.\n");
calloc()
ptr = (int*)calloc(n, sizeof(int));
calloc()
// Get the elements of the array
for (i = 0; i < n; ++i) { Enter number of elements: 5
ptr[i] = 5*i + 8; Memory successfully allocated
} using calloc.
The elements of the array are:
// Print the elements of the array 8,13,18,23,28
printf("The elements of the array are: ");
for (i = 0; i < n; ++i) {
printf("%d, ", ptr[i]);
}
}

return 0;
}
realloc()
“realloc” or “re-allocation” method in C 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 the default
garbage value.
realloc()
Syntax of realloc() in C
ptr = realloc(ptr, newSize);
where ptr is reallocated with new size 'newSize'.
realloc()
#include <stdio.h> // Check if the memory has been
#include <stdlib.h> successfully
int main() // allocated by malloc or not
{ if (ptr == NULL) {
int* ptr; printf("Memory not
int n, i; allocated.\n");
// Get the number of elements for exit(0);
the array }
printf("Enter number of elements: else {
%d\n", n); // Memory has been successfully
n = 5; allocated
// Dynamically allocate memory using printf("Memory successfully
calloc() allocated using calloc.\n");
ptr = (int*)calloc(n, sizeof(int));
realloc()
// Get the elements of the array // Dynamically re-allocate memory
for (i = 0; i < n; ++i) { using realloc()
ptr[i] = i + 1; ptr = (int*)realloc(ptr, n *
} sizeof(int));
// Print the elements of the array
printf("The elements of the array if (ptr == NULL) {
are: "); printf("Reallocation Failed\n");
for (i = 0; i < n; ++i) { exit(0);
printf("%d, ", ptr[i]); }
} / Memory has been successfully
allocated
// Get the new size for the array printf("Memory successfully re-
printf("\n\nEnter the new size of the allocated using realloc.\n");
array: %d\n", n);
n = 10;
realloc()
// Get the new elements of the array Enter number of elements: 5
for (i = 5; i < n; ++i) { Memory successfully allocated using
ptr[i] = i + 1; calloc.
} The elements of the array are: 1, 2, 3, 4, 5,

// Print the elements of the array Enter the new size of the array: 10
printf("The elements of the array are: ");Memory successfully re-allocated using
for (i = 0; i < n; ++i) { realloc.
printf("%d, ", ptr[i]); The elements of the array are: 1, 2, 3, 4, 5,
} 6, 7, 8, 9, 10,

free(ptr);
}

return 0;
}
free()
 “free” method in C is 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.
free()
Syntax of free() in C
free(ptr);
free()
#include <stdio.h> // Dynamically allocate memory using calloc()
#include <stdlib.h> ptr1 = (int*)calloc(n, sizeof(int));
int main()
// Check if the memory has been successfully
{
// allocated by malloc or not
int *ptr, *ptr1;
if (ptr == NULL || ptr1 == NULL) {
int n, i; printf("Memory not allocated.\n");
// Get the number of elements for the exit(0);
array }
n = 5; else {
printf("Entered number of elements:
%d\n", n); // Memory has been successfully allocated
// Dynamically allocate memory using printf("Memory successfully allocated
using malloc.\n");
malloc()
ptr = (int*)malloc(n * sizeof(int));
free()
// Free the memory Enter number of elements: 5
free(ptr); Memory successfully allocated using malloc.
printf("Malloc Memory successfully Malloc Memory successfully freed.
freed.\n");
Memory successfully allocated using calloc.
// Memory has been successfully
Calloc Memory successfully freed.
allocated
printf("\nMemory successfully
allocated using calloc.\n");
// Free the memory
free(ptr1);
printf("Calloc Memory successfully
freed.\n");
}
return 0;
}
Application of C
Operating systems: C is widely used for developing operating
systems such as Unix, Linux, and Windows.
Embedded systems: C is a popular language for developing
embedded systems such as microcontrollers, microprocessors, and
other electronic devices.
System software: C is used for developing system software such as
device drivers, compilers, and assemblers.
Networking: C is widely used for developing networking applications
such as web servers, network protocols, and network drivers.
Database systems: C is used for developing database systems such as
Oracle, MySQL, and PostgreSQL.
Application of C
Gaming: C is often used for developing computer games due to its
ability to handle low-level hardware interactions.
Artificial Intelligence: C is used for developing artificial intelligence
and machine learning applications such as neural networks and
deep learning algorithms.
Scientific applications: C is used for developing scientific
applications such as simulation software and numerical analysis
tools.
Financial applications: C is used for developing financial
applications such as stock market analysis and trading systems.
www.paruluniversity.ac.in

You might also like