SlideShare a Scribd company logo
Dynamic Memory & Linked List
Prepared by: Afaq Mansoor Khan
BSSE III- Group A
Session 2017-21
IMSciences, Peshawar.
Last Lecture Summary
• Selection Sort,
• Insertion Sort,
• Algorithms Analysis
• Merge Sort Algorithm,
• Merge Sort Analysis
Objectives Overview
• Dynamic Memory Allocation
• Introduction to Linked List
4
Problem with Arrays
• Sometimes
▫ Amount of data cannot be predicted beforehand
▫ Number of data items keeps changing during program execution
• Example: Search for an element in an array of N elements
• One solution: find the maximum possible value of N and allocate an
array of N elements
▫ Wasteful of memory space, as N may be much smaller in some
executions
▫ Example: maximum value of N may be 10,000, but a particular
run may need to search only among 100 elements
 Using array of size 10,000 always wastes memory in most cases
Better Solution
• Dynamic memory allocation
▫ Know how much memory is needed after the program is
run
 Example: ask the user to enter from keyboard
▫ Dynamically allocate only the amount of memory needed
5
Memory Allocation
There are essentially two types of memory allocation
1. Static
2. Dynamic
Memory Allocation - Static
Static – Done by the compiler automatically (implicitly).
o Global variables or objects -- memory is allocated at the start
of the program, and freed when program exits; alive
throughout program execution
1. Can be access anywhere in the program.
o Local variables (inside a routine) – memory is allocated when
the routine starts and freed when the routine returns.
1. A local variable cannot be accessed from another routine.
o Allocation and free are done implicitly.
o No need to explicitly manage memory is nice, but has
limitations!
1. Using static allocation, the array size must be fixed.
Memory Allocation - Dynamic
Wouldn’t it be nice to be able to have an array whose
size can be adjusted depending on needs.
Dynamic memory allocation deals with this situation.
Dynamic – Done explicitly by programmer.
Programmer explicitly requests the system to allocate
memory and return starting address of memory allocated
(what is this?). This address can be used by the
programmer to access the allocated memory.
When done using memory, it must be explicitly freed.
Explicitly allocating memory in C++:
The ‘new’ Operator
• Used to dynamically allocate memory
• Can be used to allocate a single variable/object or an
array of variables/objects
• The new operator returns pointer to the type
allocated
• Before the assignment, the pointer may or may not
point to a legitimate memory
• After the assignment, the pointer points to a
legitimate memory.
Dynamic Memory & Linked Lists
Explicitly freeing memory in C++:
The ‘delete’ Operator
• Used to free memory allocated with new operator
• The delete operator should be called on a pointer to
dynamically allocated memory when it is no longer needed
• Can delete a single variable/object or an array
delete PointerName;
delete [] ArrayName;
• After delete is called on a memory region, that region should
no longer be accessed by the program
• Convention is to set pointer to deleted memory to NULL
Any new must have a corresponding delete --- if not, the
program has memory leak.
New and delete may not be in the same routine.
Dynamic Memory & Linked Lists
Example
• int main()
• {
• // Below variables are allocated memory
• // dynamically.
• int *ptr1 = new int;
• int *ptr2 = new int[10];
•
• // Dynamically allocated memory is
• // deallocated
• delete ptr1;
• delete [] ptr2;
• }
The Heap
• Large area of memory controlled by the runtime
system that is used to grant dynamic memory
requests.
• It is possible to allocate memory and “lose” the
pointer to that region without freeing it. This is
called a memory leak.
• A memory leak can cause the heap to become full
• If an attempt is made to allocate memory from the
heap and there is not enough, an exception is
generated (error)
Why use dynamic memory allocation?
• Allows data (especially arrays) to take on variable
sizes (e.g. ask the user how many numbers to store,
then generate an array of integers exactly that size).
• Allows locally created variables to live past end of
routine.
• Allows us to create many structures used in Data
Structures and Algorithms
The . and -> operators
• The dot operator is used to access an object’s members
▫ M1.Simplify();
▫ M1.num = 5;
• But how do we access an objects members if we only
have a pointer to the object?
• If we have M1_ptr = &M1, Perhaps we would use
(*(M1_ptr)).Simplify()
• A shorthand for this is the arrow operator
• M1_ptr->Simplify() is equivalent to(*(M1_ptr)).Simplify()
Introduction to Linked Lists
Linked Lists
• Linked List is a very commonly used linear data
structure which consists of group of nodes in a
sequence.
• Each node holds its own data and the address of the
next node hence forming a chain like structure.
• Linked Lists are used to create trees and graphs.
Linked List Representation
Types of Linked Lists
There are 3 different implementations of Linked List
available, they are:
1. Singly Linked List
2. Doubly Linked List
3. Circular Linked List
Singly Linked List
• Singly linked lists contain nodes which have a data
part as well as an address part i.e. next, which points
to the next node in the sequence of nodes.
• The operations we can perform on singly linked lists
are insertion, deletion and traversal.
Dynamic Memory & Linked Lists
Doubly Linked List
• In a doubly linked list, each node contains a data part
and two addresses, one for the previous node and
one for the next node.
Dynamic Memory & Linked Lists
Circular Linked List
• In circular linked list the last node of the list holds
the address of the first node hence forming a circular
chain.
Dynamic Memory & Linked Lists
Advantages of Linked Lists
• They are a dynamic in nature which allocates the
memory when required.
• Insertion and deletion operations can be easily
implemented.
• Linked List reduces the access time.
Disadvantages of Linked Lists
• The memory is wasted as pointers require extra
memory for storage.
• No element can be accessed randomly; it has to
access each node sequentially.
• Reverse Traversing is difficult in linked list.
• Not cache friendly. Since array elements are
contiguous locations, there is locality of reference
which is not there in case of linked lists.
Applications of Linked Lists
• Linked lists are used to implement stacks, queues,
graphs, etc. Linked lists let you insert elements at the
beginning and end of the list. In Linked Lists we don't
need to know the size in advance
Array vs Linked List
• Both Linked List and Array are used to store linear
data of similar type, but an array consumes
contiguous memory locations allocated at compile
time, i.e. at the time of declaration of array, while for
a linked list, memory is assigned as and when data is
added to it, which means at runtime.
On the left, we have Array and on the right, we have
Linked List.
Summary
• Dynamic Memory Allocation
▫ New Operator
▫ Delete Operator
▫ Heap
▫ dot . and -> operators
• Introduction to Linked List
▫ Types
▫ Advantages
▫ Disadvantages
▫ Applications
▫ Difference between Array and Linked List
References
• https://www.geeksforgeeks.org/what-is-dynamic-
memory-allocation/
• https://www.geeksforgeeks.org/new-and-delete-
operators-in-cpp-for-dynamic-memory/
• https://www.youtube.com/watch?v=texoDnnzWao
• https://www.studytonight.com/data-
structures/introduction-to-linked-list
• https://www.geeksforgeeks.org/linked-list-set-1-
introduction/

More Related Content

PPT
Insertion sort bubble sort selection sort
PDF
Quick sort algorithn
PPTX
Quick_sort1.pptx
PDF
Singly Linked List
PDF
Linked list implementation of Queue
PPTX
Job sequencing with Deadlines
PPT
Hash table
PDF
I.BEST FIRST SEARCH IN AI
Insertion sort bubble sort selection sort
Quick sort algorithn
Quick_sort1.pptx
Singly Linked List
Linked list implementation of Queue
Job sequencing with Deadlines
Hash table
I.BEST FIRST SEARCH IN AI

What's hot (20)

PPTX
Overfitting.pptx
PPTX
Insertion sort
PDF
AI_unit IV Full Notes.pdf
PPTX
AI_Session 4 Uniformed search strategies.pptx
PPTX
Bucket sort
PPTX
uninformed search part 2.pptx
PPTX
AI_Session 3 Problem Solving Agent and searching for solutions.pptx
PPT
2-Agents- Artificial Intelligence
PPTX
Knowledge based agents
PPTX
Circular Queue data structure
PPTX
Associative Memory in Computer architecture
PPTX
Uninformed search /Blind search in AI
PDF
Binary search tree operations
PPT
Merge sort
PPTX
Quick sort algorithm using slide presentation , Learn selection sort example ...
DOCX
Selection sort lab mannual
PPTX
Different types of Shoring Algorithms with Animation
PPTX
Quick sort
PPTX
Analysis of Algorithm - Binary Search.pptx
Overfitting.pptx
Insertion sort
AI_unit IV Full Notes.pdf
AI_Session 4 Uniformed search strategies.pptx
Bucket sort
uninformed search part 2.pptx
AI_Session 3 Problem Solving Agent and searching for solutions.pptx
2-Agents- Artificial Intelligence
Knowledge based agents
Circular Queue data structure
Associative Memory in Computer architecture
Uninformed search /Blind search in AI
Binary search tree operations
Merge sort
Quick sort algorithm using slide presentation , Learn selection sort example ...
Selection sort lab mannual
Different types of Shoring Algorithms with Animation
Quick sort
Analysis of Algorithm - Binary Search.pptx
Ad

Similar to Dynamic Memory & Linked Lists (20)

PPTX
IN the name of Great Allah Linked list.pptx
PPTX
Data Structures-UNIT Four_Linked_List.pptx
PPTX
Unit 2 linear data structures
PPTX
Linked List.pptx
PPTX
Dynamic memory allocation and linked lists
PDF
06 linked list
PPT
PPT
PPT
Lecture 2b lists
PPT
GLA University is inviting you to a scheduled Zoom meeting
PDF
linked_list.pdf [for undergraduate students
PPTX
mbit_Unit-2_Linked List.pptx
PDF
Link list
PPTX
Arrays and linked lists
PDF
Linked list basics
PPTX
Linked lists linked lists vs Arrays.pptx
PPTX
Data Structure and Algorithms by Sabeen Memon03.pptx
PPTX
linked list.pptx
PPTX
Lecture 2 - Linear Data Structures & Implementation.pptx
PPTX
data structure notes for engineering DSA3.pptx
IN the name of Great Allah Linked list.pptx
Data Structures-UNIT Four_Linked_List.pptx
Unit 2 linear data structures
Linked List.pptx
Dynamic memory allocation and linked lists
06 linked list
Lecture 2b lists
GLA University is inviting you to a scheduled Zoom meeting
linked_list.pdf [for undergraduate students
mbit_Unit-2_Linked List.pptx
Link list
Arrays and linked lists
Linked list basics
Linked lists linked lists vs Arrays.pptx
Data Structure and Algorithms by Sabeen Memon03.pptx
linked list.pptx
Lecture 2 - Linear Data Structures & Implementation.pptx
data structure notes for engineering DSA3.pptx
Ad

More from Afaq Mansoor Khan (20)

PPTX
Feature Selection - Natural Language Processing
PPTX
WiFi vs LiFi - A Comparison
PPTX
Role of Electronic Media in Pakistan
PPTX
Agile Testing - Approach and Strategies
PPTX
Ethical Hacking - An Overview
PPTX
Software Architecture Design Decisions
PPTX
How to Design an Algorithm
PPTX
Software Quality Qssurance, Scrum and Linkedin
PPTX
Quick sort
PPTX
.Physics presentation - Asteroids
PPTX
Graph Data Structure
PPTX
AVL Tree Data Structure
PPTX
Binary tree
PPTX
Queue Data Structure
PPTX
Prefix, Infix and Post-fix Notations
PPTX
Stack Data Structure
PPTX
Doubly & Circular Linked Lists
PPTX
Linked List - Insertion & Deletion
PPTX
Sorting Algorithms
PPTX
Recursion and Sorting Algorithms
Feature Selection - Natural Language Processing
WiFi vs LiFi - A Comparison
Role of Electronic Media in Pakistan
Agile Testing - Approach and Strategies
Ethical Hacking - An Overview
Software Architecture Design Decisions
How to Design an Algorithm
Software Quality Qssurance, Scrum and Linkedin
Quick sort
.Physics presentation - Asteroids
Graph Data Structure
AVL Tree Data Structure
Binary tree
Queue Data Structure
Prefix, Infix and Post-fix Notations
Stack Data Structure
Doubly & Circular Linked Lists
Linked List - Insertion & Deletion
Sorting Algorithms
Recursion and Sorting Algorithms

Recently uploaded (20)

PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PPTX
L1 - Introduction to python Backend.pptx
PPTX
Essential Infomation Tech presentation.pptx
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PPTX
ai tools demonstartion for schools and inter college
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PDF
Digital Strategies for Manufacturing Companies
PDF
Softaken Excel to vCard Converter Software.pdf
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PPTX
history of c programming in notes for students .pptx
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
System and Network Administration Chapter 2
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
wealthsignaloriginal-com-DS-text-... (1).pdf
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
L1 - Introduction to python Backend.pptx
Essential Infomation Tech presentation.pptx
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
ai tools demonstartion for schools and inter college
Upgrade and Innovation Strategies for SAP ERP Customers
Digital Strategies for Manufacturing Companies
Softaken Excel to vCard Converter Software.pdf
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
VVF-Customer-Presentation2025-Ver1.9.pptx
history of c programming in notes for students .pptx
PTS Company Brochure 2025 (1).pdf.......
Design an Analysis of Algorithms I-SECS-1021-03
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
Navsoft: AI-Powered Business Solutions & Custom Software Development
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
System and Network Administration Chapter 2
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf

Dynamic Memory & Linked Lists

  • 1. Dynamic Memory & Linked List Prepared by: Afaq Mansoor Khan BSSE III- Group A Session 2017-21 IMSciences, Peshawar.
  • 2. Last Lecture Summary • Selection Sort, • Insertion Sort, • Algorithms Analysis • Merge Sort Algorithm, • Merge Sort Analysis
  • 3. Objectives Overview • Dynamic Memory Allocation • Introduction to Linked List
  • 4. 4 Problem with Arrays • Sometimes ▫ Amount of data cannot be predicted beforehand ▫ Number of data items keeps changing during program execution • Example: Search for an element in an array of N elements • One solution: find the maximum possible value of N and allocate an array of N elements ▫ Wasteful of memory space, as N may be much smaller in some executions ▫ Example: maximum value of N may be 10,000, but a particular run may need to search only among 100 elements  Using array of size 10,000 always wastes memory in most cases
  • 5. Better Solution • Dynamic memory allocation ▫ Know how much memory is needed after the program is run  Example: ask the user to enter from keyboard ▫ Dynamically allocate only the amount of memory needed 5
  • 6. Memory Allocation There are essentially two types of memory allocation 1. Static 2. Dynamic
  • 7. Memory Allocation - Static Static – Done by the compiler automatically (implicitly). o Global variables or objects -- memory is allocated at the start of the program, and freed when program exits; alive throughout program execution 1. Can be access anywhere in the program. o Local variables (inside a routine) – memory is allocated when the routine starts and freed when the routine returns. 1. A local variable cannot be accessed from another routine. o Allocation and free are done implicitly. o No need to explicitly manage memory is nice, but has limitations! 1. Using static allocation, the array size must be fixed.
  • 8. Memory Allocation - Dynamic Wouldn’t it be nice to be able to have an array whose size can be adjusted depending on needs. Dynamic memory allocation deals with this situation. Dynamic – Done explicitly by programmer. Programmer explicitly requests the system to allocate memory and return starting address of memory allocated (what is this?). This address can be used by the programmer to access the allocated memory. When done using memory, it must be explicitly freed.
  • 9. Explicitly allocating memory in C++: The ‘new’ Operator • Used to dynamically allocate memory • Can be used to allocate a single variable/object or an array of variables/objects • The new operator returns pointer to the type allocated • Before the assignment, the pointer may or may not point to a legitimate memory • After the assignment, the pointer points to a legitimate memory.
  • 11. Explicitly freeing memory in C++: The ‘delete’ Operator • Used to free memory allocated with new operator • The delete operator should be called on a pointer to dynamically allocated memory when it is no longer needed • Can delete a single variable/object or an array delete PointerName; delete [] ArrayName; • After delete is called on a memory region, that region should no longer be accessed by the program • Convention is to set pointer to deleted memory to NULL Any new must have a corresponding delete --- if not, the program has memory leak. New and delete may not be in the same routine.
  • 13. Example • int main() • { • // Below variables are allocated memory • // dynamically. • int *ptr1 = new int; • int *ptr2 = new int[10]; • • // Dynamically allocated memory is • // deallocated • delete ptr1; • delete [] ptr2; • }
  • 14. The Heap • Large area of memory controlled by the runtime system that is used to grant dynamic memory requests. • It is possible to allocate memory and “lose” the pointer to that region without freeing it. This is called a memory leak. • A memory leak can cause the heap to become full • If an attempt is made to allocate memory from the heap and there is not enough, an exception is generated (error)
  • 15. Why use dynamic memory allocation? • Allows data (especially arrays) to take on variable sizes (e.g. ask the user how many numbers to store, then generate an array of integers exactly that size). • Allows locally created variables to live past end of routine. • Allows us to create many structures used in Data Structures and Algorithms
  • 16. The . and -> operators • The dot operator is used to access an object’s members ▫ M1.Simplify(); ▫ M1.num = 5; • But how do we access an objects members if we only have a pointer to the object? • If we have M1_ptr = &M1, Perhaps we would use (*(M1_ptr)).Simplify() • A shorthand for this is the arrow operator • M1_ptr->Simplify() is equivalent to(*(M1_ptr)).Simplify()
  • 18. Linked Lists • Linked List is a very commonly used linear data structure which consists of group of nodes in a sequence. • Each node holds its own data and the address of the next node hence forming a chain like structure. • Linked Lists are used to create trees and graphs.
  • 20. Types of Linked Lists There are 3 different implementations of Linked List available, they are: 1. Singly Linked List 2. Doubly Linked List 3. Circular Linked List
  • 21. Singly Linked List • Singly linked lists contain nodes which have a data part as well as an address part i.e. next, which points to the next node in the sequence of nodes. • The operations we can perform on singly linked lists are insertion, deletion and traversal.
  • 23. Doubly Linked List • In a doubly linked list, each node contains a data part and two addresses, one for the previous node and one for the next node.
  • 25. Circular Linked List • In circular linked list the last node of the list holds the address of the first node hence forming a circular chain.
  • 27. Advantages of Linked Lists • They are a dynamic in nature which allocates the memory when required. • Insertion and deletion operations can be easily implemented. • Linked List reduces the access time.
  • 28. Disadvantages of Linked Lists • The memory is wasted as pointers require extra memory for storage. • No element can be accessed randomly; it has to access each node sequentially. • Reverse Traversing is difficult in linked list. • Not cache friendly. Since array elements are contiguous locations, there is locality of reference which is not there in case of linked lists.
  • 29. Applications of Linked Lists • Linked lists are used to implement stacks, queues, graphs, etc. Linked lists let you insert elements at the beginning and end of the list. In Linked Lists we don't need to know the size in advance
  • 30. Array vs Linked List • Both Linked List and Array are used to store linear data of similar type, but an array consumes contiguous memory locations allocated at compile time, i.e. at the time of declaration of array, while for a linked list, memory is assigned as and when data is added to it, which means at runtime.
  • 31. On the left, we have Array and on the right, we have Linked List.
  • 32. Summary • Dynamic Memory Allocation ▫ New Operator ▫ Delete Operator ▫ Heap ▫ dot . and -> operators • Introduction to Linked List ▫ Types ▫ Advantages ▫ Disadvantages ▫ Applications ▫ Difference between Array and Linked List
  • 33. References • https://www.geeksforgeeks.org/what-is-dynamic- memory-allocation/ • https://www.geeksforgeeks.org/new-and-delete- operators-in-cpp-for-dynamic-memory/ • https://www.youtube.com/watch?v=texoDnnzWao • https://www.studytonight.com/data- structures/introduction-to-linked-list • https://www.geeksforgeeks.org/linked-list-set-1- introduction/