SlideShare a Scribd company logo
DATA STRUCTURES (CSE1101)
EVEN SEMESTER 2023
MODULE-I: INTRODUCTION TO DATA STRUCTURE
DATA STRUCTURES
✔ A data structure is a scheme for organizing data in the memory of a computer
✔ The way in which the data is organized affects the performance of a program for different
tasks
✔Every data structure has its own strengths, and weaknesses.
✔Also, every data structure specially suits to specific problem types depending upon the
operations performed and the data organization.
2
DATA TYPES
3
 Data type is a storage format in which a variable can store data to perform
specific operations.
 A type of data that can be used in different computer programs such as
integer, char, float, etc.
 To use a variable in a program, it must be defined as a data type.
 The size of all variables and constants are determined by data types.
DATA OBJECT
• A list of elements is called as data object.
• Data object is a storage region that contains a value or group of values.
• Each value can be accessed by its identifier.
• Each object has a unique data type. It is a unique location in memory to store value.
• Data object: a set of elements (D) of a specific data type which may be finite or
infinite
4
DATA TYPES
5
ABSTRACT DATA TYPE (ADT)
• Data structure defines physical form whereas ADT defines logical form of data type.
• The abstract data type is a special kind of data type, whose behaviour is defined by:
• a set of values D
• a set of operations O
• set of axioms A that describes rules of governing the interactions of operation
Why abstract?
How those operations are working that is totally hidden from the user (abstract)
6
ADT EXAMPLES
List operations −
• insert(x): to insert one element into the list
• remove(x): to remove given element from the list
• size(): to get number of elements present into the list
• get(i): to get element at position i
• replace(x, y):to replace x with y value
7
Primitive operations performed on data Structure
1. Traversal: To process, each data element accessed exactly once
2. Insertion: To add new data element in given lists of data elements
3. Deletion: To delete existing data elements from given lists of data elements.
4. Searching: If a data element exists in the list of elements then return the location of that data
element.
5. Sorting: To arrange the data elements in ascending or descending order in the case of
numerical values and in the dictionary order in case of alphanumeric values.
6. Merging: To combine two sorted data structures into a single sorted form of data structure.
8
TYPES OF DATA STRUCTURES
 Primitive Data Structures
 Primitive data structures are operated directly according to machine instruction and
are basic structures used in programming languages.
 Different computer uses different representations of primitive data structures.
 Examples: int, float, char, constants, pointers, etc.
 Non-primitive Data Structures
 Non-primitive data structures are more sophisticated data structures used in computer
science. These data structures are derived from primitive data structures.
 They structure homogeneous and heterogeneous data elements.
 Examples: array, structure, list, files, etc.
9
TYPES OF DATA STRUCTURES
10
 Linear Data Structures
 Data is arranged in sequence, and every element is related to its previous and next element.
 All elements are stored sequentially, so traversing of elements is possible one after another.
 It uses a one-to-one relationship so that at a time, one element is allowed to access.
 Even though memory utilization is inefficient, implementation of these data structures is easy.
 Examples: array, stack, queue, linked list etc
 Non-Linear Data Structures
 Data is not arranged in sequence and every element is related to many elements.
 Elements are non-sequential, so traversing one after the another element is not possible.
 It uses one-to-many relationships.
 Even though memory utilization is efficient, implementation of these data structures is difficult.
 Examples: tree, graph etc.
LINEAR AND NON-LINEAR DATA STRUCTURES
11
Source: https://techvidvan.com/tutorials/data-structure-in-java/
LINEAR AND NON-LINEAR DATA STRUCTURES
12
Source: https://techvidvan.com/tutorials/data-structure-in-java/
Source: http://123codegenerator.blogspot.com/2010/05/array-data-type.html
Source: https://www.geeksforgeeks.org/data-structures/linked-list/
Source: https://www.softwaretestinghelp.com/stack-in-cpp/
source: https://medium.com/@pleelaprasad/data-structures-3515827fbf89
++++++++++++++++
Source: https://dzone.com/articles/an-overview-to-working-of-graphs-in-data-structure
Source: https://towardsdatascience.com/8-useful-tree-data-structures-worth-knowing-8532c7231e8c
Source: https://morioh.com/p/e9d17b46e8dd
TYPES OF DATA STRUCTURES
16
 Physical data Structures
 Physical data structures decide how memory is allocated and define how memory is
organized
 The basic purpose is to store data in memory.
 Examples: array, linked list, etc
 Logical data structures
 Logical data structures define disciplines.
 They are used to express the relationship between data elements and define how to
utilize those values, and how operations are applied on them.
 Examples: stack, queue, tree, graph, hash table etc
TYPES OF DATA STRUCTURES
17
 Static Data Structures
 A data structure in which the size of the structure remains fixed.
 Without changing the allocated memory space, the contents of the data structure can be
modified.
 Its declaration is required at the start of the program as memory is allocated at compile time.
 In these data structures, accessing elements is easier.
 Examples: array
 Dynamic data structures
 A structure in which the size of the structure is not fixed and is allocated dynamically
 As the contents of the data structure can be modified during the operations performed on it,
the size of memory can be changed.
 Its declaration is required when we want to perform operations on it, so memory is allocated
at run time.
 These data structures are flexible.
 Examples: stack, queue, linked list, etc
TYPES OF DATA STRUCTURES
18
 Linked data structures
 A data structure in which elements may or may not be stored in consecutive
memory locations
 To go to a particular element, all elements preceding the element are required
to access.
 Its size is not fixed, so a different amount of time is required to access each
element.
 In insertion and deletion operations, movement of all elements is not required.
 It uses a pointer, so according to programmer requirement, it can be extended
or reduced.
 Examples: linked list etc.
TYPES OF DATA STRUCTURES
 Sequential data structures
 A data structure in which elements are stored consecutive memory locations.
 To go to a particular element, direct access is possible.
 Its size is fixed, so the same amount of time is required to access each
element.
 In insertion and deletion operations, movement of elements is required.
 According to the programmer’s requirement, it can not be extended or
reduced.
 Examples: array
19
CASE STUDY (NOT IN EXAM’S SYLLABUS)
• Memory Allocation
20
MEMORY ALLOCATION
✔Memory allocation in programming is very important for storing values when you
assign them to variables
✔allocates memory for the variables declared by a programmer via the compiler
✔allocation is done either before or at the time of program execution
21
WAYS FOR MEMORY ALLOCATION
• Compile time allocation or static allocation of memory:
✔ The memory for named variables is allocated by the compiler.
✔ The memory allocated by the compiler is allocated on the stack.
✔ Exact size and storage must be known at compile time.
✔ for array declaration, the size has to be constant.
• Runtime allocation or dynamic allocation of memory:
✔ The memory is allocated at runtime.
✔ The memory space is allocated dynamically within the program run.
✔ Dynamically allocated memory is allocated on the heap.
✔ The exact space or number of the data items does not have to be known by the compiler in advance.
✔ Pointers play a major role for dynamic memory allocation.
22
DYNAMIC MEMORY ALLOCATION
• Memory de-allocation is also an important part of this concept
• The "clean-up" of storage space is done for variables or for other data storage.
• It is the job of the programmer to de-allocate dynamically created space.
• For de-allocating dynamic memory, delete operator is used.
• In other words, dynamic memory Allocation refers to performing memory
management for dynamic memory allocation manually.
23
MEMORY PARTS IN C++ PROGRAM
• stack:
✔ All variables declared inside any function takes up memory from the stack.
• heap:
✔ It is the unused memory of the program and can be used to dynamically allocate the
memory at runtime.
24
THE “NEW” OPERATOR
• To allocate space dynamically, unary operator new is used , followed by the
type being allocated for memory.
✔new int; //dynamically allocates memory for an integer type
✔new double; // dynamically allocates memory an double type
✔new int[30]; // dynamically allocates memory for integer array
25
ALLOCATING SPACE FOR NEW
• int * ptr=NULL; // declares a pointer ptr
• ptr = new int; // dynamically allocate an int for loading the address in ptr
• double * i; // declares a pointer i
• i = new double; // dynamically allocate a double and loading the address in i
26
In the above example,
✔ we have declared a pointer variable ‘ptr’ to integer and initialized it to null.
✔ Using the “new” operator memory will be allocate to the “ptr” variable.
DYNAMIC MEMORY ALLOCATION FOR ARRAYS
• To allocate memory for an array of characters, i.e., a string of 50 characters. Using that
same syntax, memory can be allocated dynamically
• char* str = NULL; // Pointer initialized with NULL
• str= new char[50]; // Dynamic Allocation will be done
• Here, new operator allocates 50 continuous elements of type characters to the pointer
variable str and returns the pointer to the first element of str.
27
DYNAMIC MEMORY ALLOCATION FOR
LINKED LIST
Let us take a structure of a linked list node:
struct node
{
int data;
node *next;
};
node *temp=new node; // dynamic memory allocation
✔ Memory is allocated required for a node by the new operator.
✔ ‘temp’ points to a node (or space allocated for the node).
28
EXAMPLE: LINKED LIST USING NEW OPERATOR
#include <iostream>
using namespace std;
struct node
{
int data;
node *next;
};
class linked_list
{
private:
node *head,*tail;
public:
linked_list()
29
{
head = NULL;
tail = NULL;
}
void add_node(int n)
{
node *tmp = new node;
tmp->data = n;
tmp->next = NULL;
if(head == NULL)
{
head = tmp;
tail = tmp;
}
else
{
tail->next = tmp;
tail = tail->next;
}
}
};
int main()
{
linked_list a;
a.add_node(1);
a.add_node(2);
return 0;
}
Source: https://www.codesdope.com/blog/article/c-linked-lists-in-c-singly-linked-list/
THE DELETE OPERATOR
• The memory allocated dynamically using the new operator has to be freed explicitly by the
programmer. For this purpose, the “delete” operator is used
• The general syntax of the delete operator is:
delete pointer_variable;
• So memory allocated to the ptr variable can be freed as:
delete ptr;
• This statement frees the memory allocated to the variable “ptr” back to the memory pool.
• delete operator can also be used to free the memory allocated to arrays.
30
THE DELETE OPERATOR
• the memory allocated to the array str above can be freed as follows:
delete[] str;
• Note the subscript operator used with the delete operator.
• This is because, as we have allocated the array of elements, we need to free all the locations.
• Instead, if the following statement had executed:
delete str;
• the above statement will only delete the first element of the array.
• Using subscript “[]” all the memory allocated is to be freed.
31
MALLOC()
• The malloc() function from C, also exists in C++,
• But it is recommended to avoid using malloc() function.
• malloc() allocates requested size of bytes and returns a pointer to the first
byte of allocated space.
• The main benefit of new over malloc() is that new doesn't just allocate memory,
it constructs objects which is a prime concept of C++.
32
DYNAMIC MEMORY ALLOCATION PROGRAMMING EXAMPLE:1
#include <iostream>
using namespace std;
int main()
{
int* i= NULL;
i= new int;
*i= 5;
cout << "Value is : " << *i<< endl; delete val;
}
33
EXAMPLE 2 : NEW AND DELETE OPERATORS IN C++.
34
#include <iostream>
#include <string>
using namespace std;
int main()
{
int *p= NULL;
i= new int();
int *a= new int(10);
if(!p)
{
cout<<"bad memory allocation"<<endl;
}
else
{
cout<<"memory allocated successfully"<<endl;
*p= 5;
cout<<"*p= "<<*p<<endl;
cout<<"*a= "<<*a<<endl;
}
double *arr= NULL;
arr= new double[10];
if(!arr)
{cout<<"memory not allocated"<<endl;}
else
{
for(int i=0;i<5;i++)
arr] = i+1;
cout<<“Array values : ";
for(int i=0;i<5;i++)
cout<<arr[i]<<"t";
}
delete p;
delete a;
delete[] arr;
return 0;
PROGRAM OUTPUT
35
memory allocated successfully
*i= 5
*a= 10
Array values:1 2 3 4 5
THE MAJOR DIFFERENCES BETWEEN STATIC AND DYNAMIC MEMORY
ALLOCATIONS
Static Memory Allocation Dynamic Memory Allocation
Memory Allocates variables permanently Memory Allocates variables only if program
unit gets active
Memory Allocation is done before program
execution
Memory Allocation is done during program
execution
Use stack data structure for implementation Use heap data structure for implementation
Less efficient More efficient
no Memory reusable memory reusable and can be freed when not
required
https://www.w3schools.in/difference-in-static-and-dynamic-memory-allocation/
REFERENCES
• Robert Lafore, Object Oriented Programming in C++, SAMS, 2002
• https://www.softwaretestinghelp.com/new-delete-operators-in-cpp/
• https://www.softwaretestinghelp.com/stack-in-cpp/
• https://www.geeksforgeeks.org/
• https://www.tutorialspoint.com/abstract-data-type-in-data-structures
37

More Related Content

PPT
CS301-lec01.ppt
PPT
Data structures cs301 power point slides lecture 01
PPT
Data Structure Lec #1
PDF
Data Structures & Recursion-Introduction.pdf
PPT
Data structure and algorithm with java by shikra
PDF
Unit 1-Introduction to Data Structures-BCA.pdf
PPTX
DS-UNIT 1 FINAL (2).pptx
PPTX
Data Structures_Introduction
CS301-lec01.ppt
Data structures cs301 power point slides lecture 01
Data Structure Lec #1
Data Structures & Recursion-Introduction.pdf
Data structure and algorithm with java by shikra
Unit 1-Introduction to Data Structures-BCA.pdf
DS-UNIT 1 FINAL (2).pptx
Data Structures_Introduction

Similar to Module 1 Intro to Data Structures CSE 1101.pptx (20)

PPSX
Data Structure using C by Dr. K Adisesha .ppsx
PDF
2. Introduction to Data Structure.pdf
PPTX
Introduction to datastructures presentation
PDF
Unit-I PPT hususi sisooshsgv. Eijeieieooekejj
PPTX
Introduction to Data Structures and their importance
PPTX
Introduction to data structures (ss)
PPTX
DSA - Copy.pptx
PDF
Data structure using c++
PPTX
1-Introduction to Data Structures beginner.pptx
PPT
DATA STRUCTURE AND ALGORITHMS
PPTX
UNIT 3.pptx-Data Structures definition with examples
PPTX
DataStructurePpt-01.pptxEngineering data structure notes
PDF
Data Structure Ppt for our engineering college industrial training.
PPTX
Introduction to Data Structures
PPTX
Unit-1 DataStructure Intro.pptx
PPTX
DataStructureccvdgddfffdesddsssdssPpt.pptx
PPTX
Data struchers and algorithms
PPTX
ntroduction of Algorithms, Analysing Algorithms. Arrays: Sparse Matrices - Re...
PPTX
DATA STRUCTURE AND COMPUTER ALGORITHMS LECTURE 1
PPT
Data Structure using C by Dr. K Adisesha .ppsx
2. Introduction to Data Structure.pdf
Introduction to datastructures presentation
Unit-I PPT hususi sisooshsgv. Eijeieieooekejj
Introduction to Data Structures and their importance
Introduction to data structures (ss)
DSA - Copy.pptx
Data structure using c++
1-Introduction to Data Structures beginner.pptx
DATA STRUCTURE AND ALGORITHMS
UNIT 3.pptx-Data Structures definition with examples
DataStructurePpt-01.pptxEngineering data structure notes
Data Structure Ppt for our engineering college industrial training.
Introduction to Data Structures
Unit-1 DataStructure Intro.pptx
DataStructureccvdgddfffdesddsssdssPpt.pptx
Data struchers and algorithms
ntroduction of Algorithms, Analysing Algorithms. Arrays: Sparse Matrices - Re...
DATA STRUCTURE AND COMPUTER ALGORITHMS LECTURE 1
Ad

More from bansidhar11 (13)

PPT
Introduction to MIPS Computer Architecture
PPT
MIPS ALU and Datapath: Design and analysis
PPT
Introduction to MIPS Computer Architecture
PPT
Memory organization including cache and RAM.ppt
PPT
Memory Organization and Cache mapping.ppt
PPTX
RowHammerUnderReducedWordlineVoltage_dsn22-lightning-talk.pptx
PPT
Side-Channel Attacks in Memory: A threat
PPTX
On_Chip_Networks_Digital_Forensics_Presentation.pptx
PPT
Introduction to Computer Organization and Architecture
PPT
Introduction to Computer Organization and Architecture
PPTX
8085 Assembly programming.pptx
PPTX
Carbon and its compounds.ppt
PPT
Introduction to MIPS Computer Architecture
MIPS ALU and Datapath: Design and analysis
Introduction to MIPS Computer Architecture
Memory organization including cache and RAM.ppt
Memory Organization and Cache mapping.ppt
RowHammerUnderReducedWordlineVoltage_dsn22-lightning-talk.pptx
Side-Channel Attacks in Memory: A threat
On_Chip_Networks_Digital_Forensics_Presentation.pptx
Introduction to Computer Organization and Architecture
Introduction to Computer Organization and Architecture
8085 Assembly programming.pptx
Carbon and its compounds.ppt
Ad

Recently uploaded (20)

DOCX
573137875-Attendance-Management-System-original
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PPTX
Geodesy 1.pptx...............................................
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PPTX
Welding lecture in detail for understanding
PPTX
web development for engineering and engineering
PPT
Project quality management in manufacturing
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PPT
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PPTX
UNIT 4 Total Quality Management .pptx
PPTX
Sustainable Sites - Green Building Construction
PDF
Digital Logic Computer Design lecture notes
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PDF
composite construction of structures.pdf
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PPT
Mechanical Engineering MATERIALS Selection
573137875-Attendance-Management-System-original
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
Operating System & Kernel Study Guide-1 - converted.pdf
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
Geodesy 1.pptx...............................................
R24 SURVEYING LAB MANUAL for civil enggi
Welding lecture in detail for understanding
web development for engineering and engineering
Project quality management in manufacturing
Foundation to blockchain - A guide to Blockchain Tech
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
UNIT-1 - COAL BASED THERMAL POWER PLANTS
UNIT 4 Total Quality Management .pptx
Sustainable Sites - Green Building Construction
Digital Logic Computer Design lecture notes
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
composite construction of structures.pdf
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
Mechanical Engineering MATERIALS Selection

Module 1 Intro to Data Structures CSE 1101.pptx

  • 1. DATA STRUCTURES (CSE1101) EVEN SEMESTER 2023 MODULE-I: INTRODUCTION TO DATA STRUCTURE
  • 2. DATA STRUCTURES ✔ A data structure is a scheme for organizing data in the memory of a computer ✔ The way in which the data is organized affects the performance of a program for different tasks ✔Every data structure has its own strengths, and weaknesses. ✔Also, every data structure specially suits to specific problem types depending upon the operations performed and the data organization. 2
  • 3. DATA TYPES 3  Data type is a storage format in which a variable can store data to perform specific operations.  A type of data that can be used in different computer programs such as integer, char, float, etc.  To use a variable in a program, it must be defined as a data type.  The size of all variables and constants are determined by data types.
  • 4. DATA OBJECT • A list of elements is called as data object. • Data object is a storage region that contains a value or group of values. • Each value can be accessed by its identifier. • Each object has a unique data type. It is a unique location in memory to store value. • Data object: a set of elements (D) of a specific data type which may be finite or infinite 4
  • 6. ABSTRACT DATA TYPE (ADT) • Data structure defines physical form whereas ADT defines logical form of data type. • The abstract data type is a special kind of data type, whose behaviour is defined by: • a set of values D • a set of operations O • set of axioms A that describes rules of governing the interactions of operation Why abstract? How those operations are working that is totally hidden from the user (abstract) 6
  • 7. ADT EXAMPLES List operations − • insert(x): to insert one element into the list • remove(x): to remove given element from the list • size(): to get number of elements present into the list • get(i): to get element at position i • replace(x, y):to replace x with y value 7
  • 8. Primitive operations performed on data Structure 1. Traversal: To process, each data element accessed exactly once 2. Insertion: To add new data element in given lists of data elements 3. Deletion: To delete existing data elements from given lists of data elements. 4. Searching: If a data element exists in the list of elements then return the location of that data element. 5. Sorting: To arrange the data elements in ascending or descending order in the case of numerical values and in the dictionary order in case of alphanumeric values. 6. Merging: To combine two sorted data structures into a single sorted form of data structure. 8
  • 9. TYPES OF DATA STRUCTURES  Primitive Data Structures  Primitive data structures are operated directly according to machine instruction and are basic structures used in programming languages.  Different computer uses different representations of primitive data structures.  Examples: int, float, char, constants, pointers, etc.  Non-primitive Data Structures  Non-primitive data structures are more sophisticated data structures used in computer science. These data structures are derived from primitive data structures.  They structure homogeneous and heterogeneous data elements.  Examples: array, structure, list, files, etc. 9
  • 10. TYPES OF DATA STRUCTURES 10  Linear Data Structures  Data is arranged in sequence, and every element is related to its previous and next element.  All elements are stored sequentially, so traversing of elements is possible one after another.  It uses a one-to-one relationship so that at a time, one element is allowed to access.  Even though memory utilization is inefficient, implementation of these data structures is easy.  Examples: array, stack, queue, linked list etc  Non-Linear Data Structures  Data is not arranged in sequence and every element is related to many elements.  Elements are non-sequential, so traversing one after the another element is not possible.  It uses one-to-many relationships.  Even though memory utilization is efficient, implementation of these data structures is difficult.  Examples: tree, graph etc.
  • 11. LINEAR AND NON-LINEAR DATA STRUCTURES 11 Source: https://techvidvan.com/tutorials/data-structure-in-java/
  • 12. LINEAR AND NON-LINEAR DATA STRUCTURES 12 Source: https://techvidvan.com/tutorials/data-structure-in-java/
  • 13. Source: http://123codegenerator.blogspot.com/2010/05/array-data-type.html Source: https://www.geeksforgeeks.org/data-structures/linked-list/ Source: https://www.softwaretestinghelp.com/stack-in-cpp/ source: https://medium.com/@pleelaprasad/data-structures-3515827fbf89
  • 15. TYPES OF DATA STRUCTURES 16  Physical data Structures  Physical data structures decide how memory is allocated and define how memory is organized  The basic purpose is to store data in memory.  Examples: array, linked list, etc  Logical data structures  Logical data structures define disciplines.  They are used to express the relationship between data elements and define how to utilize those values, and how operations are applied on them.  Examples: stack, queue, tree, graph, hash table etc
  • 16. TYPES OF DATA STRUCTURES 17  Static Data Structures  A data structure in which the size of the structure remains fixed.  Without changing the allocated memory space, the contents of the data structure can be modified.  Its declaration is required at the start of the program as memory is allocated at compile time.  In these data structures, accessing elements is easier.  Examples: array  Dynamic data structures  A structure in which the size of the structure is not fixed and is allocated dynamically  As the contents of the data structure can be modified during the operations performed on it, the size of memory can be changed.  Its declaration is required when we want to perform operations on it, so memory is allocated at run time.  These data structures are flexible.  Examples: stack, queue, linked list, etc
  • 17. TYPES OF DATA STRUCTURES 18  Linked data structures  A data structure in which elements may or may not be stored in consecutive memory locations  To go to a particular element, all elements preceding the element are required to access.  Its size is not fixed, so a different amount of time is required to access each element.  In insertion and deletion operations, movement of all elements is not required.  It uses a pointer, so according to programmer requirement, it can be extended or reduced.  Examples: linked list etc.
  • 18. TYPES OF DATA STRUCTURES  Sequential data structures  A data structure in which elements are stored consecutive memory locations.  To go to a particular element, direct access is possible.  Its size is fixed, so the same amount of time is required to access each element.  In insertion and deletion operations, movement of elements is required.  According to the programmer’s requirement, it can not be extended or reduced.  Examples: array 19
  • 19. CASE STUDY (NOT IN EXAM’S SYLLABUS) • Memory Allocation 20
  • 20. MEMORY ALLOCATION ✔Memory allocation in programming is very important for storing values when you assign them to variables ✔allocates memory for the variables declared by a programmer via the compiler ✔allocation is done either before or at the time of program execution 21
  • 21. WAYS FOR MEMORY ALLOCATION • Compile time allocation or static allocation of memory: ✔ The memory for named variables is allocated by the compiler. ✔ The memory allocated by the compiler is allocated on the stack. ✔ Exact size and storage must be known at compile time. ✔ for array declaration, the size has to be constant. • Runtime allocation or dynamic allocation of memory: ✔ The memory is allocated at runtime. ✔ The memory space is allocated dynamically within the program run. ✔ Dynamically allocated memory is allocated on the heap. ✔ The exact space or number of the data items does not have to be known by the compiler in advance. ✔ Pointers play a major role for dynamic memory allocation. 22
  • 22. DYNAMIC MEMORY ALLOCATION • Memory de-allocation is also an important part of this concept • The "clean-up" of storage space is done for variables or for other data storage. • It is the job of the programmer to de-allocate dynamically created space. • For de-allocating dynamic memory, delete operator is used. • In other words, dynamic memory Allocation refers to performing memory management for dynamic memory allocation manually. 23
  • 23. MEMORY PARTS IN C++ PROGRAM • stack: ✔ All variables declared inside any function takes up memory from the stack. • heap: ✔ It is the unused memory of the program and can be used to dynamically allocate the memory at runtime. 24
  • 24. THE “NEW” OPERATOR • To allocate space dynamically, unary operator new is used , followed by the type being allocated for memory. ✔new int; //dynamically allocates memory for an integer type ✔new double; // dynamically allocates memory an double type ✔new int[30]; // dynamically allocates memory for integer array 25
  • 25. ALLOCATING SPACE FOR NEW • int * ptr=NULL; // declares a pointer ptr • ptr = new int; // dynamically allocate an int for loading the address in ptr • double * i; // declares a pointer i • i = new double; // dynamically allocate a double and loading the address in i 26 In the above example, ✔ we have declared a pointer variable ‘ptr’ to integer and initialized it to null. ✔ Using the “new” operator memory will be allocate to the “ptr” variable.
  • 26. DYNAMIC MEMORY ALLOCATION FOR ARRAYS • To allocate memory for an array of characters, i.e., a string of 50 characters. Using that same syntax, memory can be allocated dynamically • char* str = NULL; // Pointer initialized with NULL • str= new char[50]; // Dynamic Allocation will be done • Here, new operator allocates 50 continuous elements of type characters to the pointer variable str and returns the pointer to the first element of str. 27
  • 27. DYNAMIC MEMORY ALLOCATION FOR LINKED LIST Let us take a structure of a linked list node: struct node { int data; node *next; }; node *temp=new node; // dynamic memory allocation ✔ Memory is allocated required for a node by the new operator. ✔ ‘temp’ points to a node (or space allocated for the node). 28
  • 28. EXAMPLE: LINKED LIST USING NEW OPERATOR #include <iostream> using namespace std; struct node { int data; node *next; }; class linked_list { private: node *head,*tail; public: linked_list() 29 { head = NULL; tail = NULL; } void add_node(int n) { node *tmp = new node; tmp->data = n; tmp->next = NULL; if(head == NULL) { head = tmp; tail = tmp; } else { tail->next = tmp; tail = tail->next; } } }; int main() { linked_list a; a.add_node(1); a.add_node(2); return 0; } Source: https://www.codesdope.com/blog/article/c-linked-lists-in-c-singly-linked-list/
  • 29. THE DELETE OPERATOR • The memory allocated dynamically using the new operator has to be freed explicitly by the programmer. For this purpose, the “delete” operator is used • The general syntax of the delete operator is: delete pointer_variable; • So memory allocated to the ptr variable can be freed as: delete ptr; • This statement frees the memory allocated to the variable “ptr” back to the memory pool. • delete operator can also be used to free the memory allocated to arrays. 30
  • 30. THE DELETE OPERATOR • the memory allocated to the array str above can be freed as follows: delete[] str; • Note the subscript operator used with the delete operator. • This is because, as we have allocated the array of elements, we need to free all the locations. • Instead, if the following statement had executed: delete str; • the above statement will only delete the first element of the array. • Using subscript “[]” all the memory allocated is to be freed. 31
  • 31. MALLOC() • The malloc() function from C, also exists in C++, • But it is recommended to avoid using malloc() function. • malloc() allocates requested size of bytes and returns a pointer to the first byte of allocated space. • The main benefit of new over malloc() is that new doesn't just allocate memory, it constructs objects which is a prime concept of C++. 32
  • 32. DYNAMIC MEMORY ALLOCATION PROGRAMMING EXAMPLE:1 #include <iostream> using namespace std; int main() { int* i= NULL; i= new int; *i= 5; cout << "Value is : " << *i<< endl; delete val; } 33
  • 33. EXAMPLE 2 : NEW AND DELETE OPERATORS IN C++. 34 #include <iostream> #include <string> using namespace std; int main() { int *p= NULL; i= new int(); int *a= new int(10); if(!p) { cout<<"bad memory allocation"<<endl; } else { cout<<"memory allocated successfully"<<endl; *p= 5; cout<<"*p= "<<*p<<endl; cout<<"*a= "<<*a<<endl; } double *arr= NULL; arr= new double[10]; if(!arr) {cout<<"memory not allocated"<<endl;} else { for(int i=0;i<5;i++) arr] = i+1; cout<<“Array values : "; for(int i=0;i<5;i++) cout<<arr[i]<<"t"; } delete p; delete a; delete[] arr; return 0;
  • 34. PROGRAM OUTPUT 35 memory allocated successfully *i= 5 *a= 10 Array values:1 2 3 4 5
  • 35. THE MAJOR DIFFERENCES BETWEEN STATIC AND DYNAMIC MEMORY ALLOCATIONS Static Memory Allocation Dynamic Memory Allocation Memory Allocates variables permanently Memory Allocates variables only if program unit gets active Memory Allocation is done before program execution Memory Allocation is done during program execution Use stack data structure for implementation Use heap data structure for implementation Less efficient More efficient no Memory reusable memory reusable and can be freed when not required https://www.w3schools.in/difference-in-static-and-dynamic-memory-allocation/
  • 36. REFERENCES • Robert Lafore, Object Oriented Programming in C++, SAMS, 2002 • https://www.softwaretestinghelp.com/new-delete-operators-in-cpp/ • https://www.softwaretestinghelp.com/stack-in-cpp/ • https://www.geeksforgeeks.org/ • https://www.tutorialspoint.com/abstract-data-type-in-data-structures 37