SlideShare ist ein Scribd-Unternehmen logo
1 von 8
Dynamic Memory Allocation
&
Linked Lists
Dynamic memory location:
The process of allocating memory at run time is known as dynamic memory
allocation.
C does not inherently have this facility. There are four library routines known as
memory management functions that can be used for allocating and freeing memory
during execution.
malloc(), calloc(), free(), realloc()
malloc: Allocating a block of memory
General form
ptr = (cast-type *) malloc(byte-size);
ptr is a pointer of type cast-type
The malloc() returns a pointer (of cast-type) to an area of memory with size byte-
size.
If there is not enough space a NULL pointer is returned.
Example
x=(int *) malloc(100*sizeof(int));
cptr=(char *) malloc(10);
st= (struct *) malloc(sizeof(struct store));
Storage space allocated dynamically has no name and therefore its contents can be accessed only through
a pointer.
calloc: Allocating multiple block of memory
While malloc() allocates a single block of storage space, calloc() allocates multiple blocks of storage, each
of the same size, and then set all bytes to zero. If there is not enough space a NULL pointer is returned.
General form
ptr=(cast-type *) calloc(n, element-size);
free( ); Releasing the used space
With dynamic run-time allocation, we need to release the space when it is not required
free(ptr);
Frees previously allocated space created by malloc() or calloc()
realloc( ); Altering the size of a block
It is likely that the previous allocated memory is not sufficient and we need
additional space for more elements.
It is also possible that the memory allocated is much larger than necessary and
we want to reduce it.
ptr=realloc(ptr, newsize);
Modifies the size of previously allocated space by malloc() or calloc().
Linked List
A list refers to a set of items organized sequentially
Linked list is a completely different way to represent a list is to make each item
in the list part of a structure that also contains a “link” to the structure
containing the next item.
A linked list is a dynamic data structures.
Dynamic Memory Allocation
Basic It is a consistent set of a fixed number of data
items.
It is an ordered set comprising a variable number
of data items.
Size Specified during declaration. No need to specify; grow and shrink during
execution
Storage
Allocation
Element location is allocated during compile time. Element position is assigned during run time.
Order of the
elements
Stored consecutively Stored randomly
Accessing the
element
Direct or randomly accessed, i.e., Specify the array
index or subscript.
Sequentially accessed, i.e., Traverse starting from
the first node in the list by the pointer.
Difference between array and linked list
Array Linked List
Insertion and
deletion of
element
Slow relatively as shifting is required. Easier, fast and efficient.
Searching Binary search and linear search linear search
Memory
required
less More
Memory
Utilization
Ineffective Efficient
Difference between array and linked list
Array Linked List
Advantages of linked list
• Can grow or shrink in size during the execution of a program
• Does not waste memory space
• Here it is not necessary to specify the number of nodes to be used in the list
• It provides flexibility is allowing the items to be rearranged efficiently
• It is easier to insert or delete items by rearranging the links
Limitation of Linked List
• The access to any arbitrary item is little cumbersome and time consuming.
• Use more storage than an array with the same number of items. This is because each item
has an additional link field.
• Linked lists are traversed in unidirection
• Complex to implement
• Sequential access to elements
• Leads to memory problems if not taken care about the pointer manipulations properly
That is, Whenever we deal with a fixed length list, it would be better to use an array rather
than a linked list.
What we can do with Linked List:-
1. Creating a list.
2. Traversing the list.
3. Counting the items in the list.
4. Printing the list (or sub list).
5. Looking up an item for editing or printing.
6. Inserting an item.
7. Deleting an item.
8. Concatenating two lists.
9. Replace() : replaces a value stored at a position with some other value.
10.Swap() : swap the values of the nodes specified by two positions.
11. Merging two linked lists into a larger list.
12.Searching for an element in a linked list.
13.Reversing a linked list.

Weitere ähnliche Inhalte

Was ist angesagt?

Presentation on Data Structure
Presentation on Data StructurePresentation on Data Structure
Presentation on Data StructureA. N. M. Jubaer
 
Data structure lecture 2
Data structure lecture 2Data structure lecture 2
Data structure lecture 2Abbott
 
DATA STRUCTURE IN C LANGUAGE
DATA STRUCTURE IN C LANGUAGEDATA STRUCTURE IN C LANGUAGE
DATA STRUCTURE IN C LANGUAGEshubhamrohiwal6
 
Visula C# Programming Lecture 5
Visula C# Programming Lecture 5Visula C# Programming Lecture 5
Visula C# Programming Lecture 5Abou Bakr Ashraf
 
Introduction to data structure by anil dutt
Introduction to data structure by anil duttIntroduction to data structure by anil dutt
Introduction to data structure by anil duttAnil Dutt
 
Array c programming
Array c programmingArray c programming
Array c programmingDevan Thakur
 
DATA STRUCTURE
DATA STRUCTUREDATA STRUCTURE
DATA STRUCTURERohit Rai
 
Data structure & its types
Data structure & its typesData structure & its types
Data structure & its typesRameesha Sadaqat
 
Presentation on array
Presentation on array Presentation on array
Presentation on array topu93
 
Introduction to Array ppt
Introduction to Array pptIntroduction to Array ppt
Introduction to Array pptsandhya yadav
 
Array in c language
Array in c languageArray in c language
Array in c languageumesh patil
 
Introduction to data structure
Introduction to data structureIntroduction to data structure
Introduction to data structureZaid Shabbir
 

Was ist angesagt? (18)

Array in c
Array in cArray in c
Array in c
 
Presentation on Data Structure
Presentation on Data StructurePresentation on Data Structure
Presentation on Data Structure
 
Data structure lecture 2
Data structure lecture 2Data structure lecture 2
Data structure lecture 2
 
DATA STRUCTURE IN C LANGUAGE
DATA STRUCTURE IN C LANGUAGEDATA STRUCTURE IN C LANGUAGE
DATA STRUCTURE IN C LANGUAGE
 
Visula C# Programming Lecture 5
Visula C# Programming Lecture 5Visula C# Programming Lecture 5
Visula C# Programming Lecture 5
 
The awesome algorithm
The awesome algorithmThe awesome algorithm
The awesome algorithm
 
Introduction to data structure by anil dutt
Introduction to data structure by anil duttIntroduction to data structure by anil dutt
Introduction to data structure by anil dutt
 
Numeric Arrays [6]
Numeric Arrays [6]Numeric Arrays [6]
Numeric Arrays [6]
 
Array c programming
Array c programmingArray c programming
Array c programming
 
Array
ArrayArray
Array
 
DATA STRUCTURE
DATA STRUCTUREDATA STRUCTURE
DATA STRUCTURE
 
Data structure & its types
Data structure & its typesData structure & its types
Data structure & its types
 
Presentation on array
Presentation on array Presentation on array
Presentation on array
 
Java part 2
Java part  2Java part  2
Java part 2
 
Introduction to Array ppt
Introduction to Array pptIntroduction to Array ppt
Introduction to Array ppt
 
Array in c language
Array in c languageArray in c language
Array in c language
 
Linked list
Linked listLinked list
Linked list
 
Introduction to data structure
Introduction to data structureIntroduction to data structure
Introduction to data structure
 

Ähnlich wie Dynamic memory allocation and linked lists

Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocationMoniruzzaman _
 
How to choose best containers in STL (C++)
How to choose best containers in STL (C++)How to choose best containers in STL (C++)
How to choose best containers in STL (C++)Sangharsh agarwal
 
Ap Power Point Chpt9
Ap Power Point Chpt9Ap Power Point Chpt9
Ap Power Point Chpt9dplunkett
 
Standard template library
Standard template libraryStandard template library
Standard template librarySukriti Singh
 
DS-UNIT 1 FINAL (2).pptx
DS-UNIT 1 FINAL (2).pptxDS-UNIT 1 FINAL (2).pptx
DS-UNIT 1 FINAL (2).pptxprakashvs7
 
Data Structures_Introduction
Data Structures_IntroductionData Structures_Introduction
Data Structures_IntroductionThenmozhiK5
 
Data Structure & aaplications_Module-1.pptx
Data Structure & aaplications_Module-1.pptxData Structure & aaplications_Module-1.pptx
Data Structure & aaplications_Module-1.pptxGIRISHKUMARBC1
 
Describes the concept of ADTS and illustrates the concept with three o.docx
Describes the concept of ADTS and illustrates the concept with three o.docxDescribes the concept of ADTS and illustrates the concept with three o.docx
Describes the concept of ADTS and illustrates the concept with three o.docxearleanp
 
Data structure Assignment Help
Data structure Assignment HelpData structure Assignment Help
Data structure Assignment HelpJosephErin
 
Data structure,abstraction,abstract data type,static and dynamic,time and spa...
Data structure,abstraction,abstract data type,static and dynamic,time and spa...Data structure,abstraction,abstract data type,static and dynamic,time and spa...
Data structure,abstraction,abstract data type,static and dynamic,time and spa...Hassan Ahmed
 
Data Structures & Algorithms Unit 1.pptx
Data Structures & Algorithms Unit 1.pptxData Structures & Algorithms Unit 1.pptx
Data Structures & Algorithms Unit 1.pptxUsriDevi1
 

Ähnlich wie Dynamic memory allocation and linked lists (20)

Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocation
 
Chap 13(dynamic memory allocation)
Chap 13(dynamic memory allocation)Chap 13(dynamic memory allocation)
Chap 13(dynamic memory allocation)
 
How to choose best containers in STL (C++)
How to choose best containers in STL (C++)How to choose best containers in STL (C++)
How to choose best containers in STL (C++)
 
Ap Power Point Chpt9
Ap Power Point Chpt9Ap Power Point Chpt9
Ap Power Point Chpt9
 
Standard template library
Standard template libraryStandard template library
Standard template library
 
DS-UNIT 1 FINAL (2).pptx
DS-UNIT 1 FINAL (2).pptxDS-UNIT 1 FINAL (2).pptx
DS-UNIT 1 FINAL (2).pptx
 
Data Structures_Introduction
Data Structures_IntroductionData Structures_Introduction
Data Structures_Introduction
 
Data Structure & aaplications_Module-1.pptx
Data Structure & aaplications_Module-1.pptxData Structure & aaplications_Module-1.pptx
Data Structure & aaplications_Module-1.pptx
 
TSAT Presentation1.pptx
TSAT Presentation1.pptxTSAT Presentation1.pptx
TSAT Presentation1.pptx
 
Any Which Array But Loose
Any Which Array But LooseAny Which Array But Loose
Any Which Array But Loose
 
DS_PPT.pptx
DS_PPT.pptxDS_PPT.pptx
DS_PPT.pptx
 
Describes the concept of ADTS and illustrates the concept with three o.docx
Describes the concept of ADTS and illustrates the concept with three o.docxDescribes the concept of ADTS and illustrates the concept with three o.docx
Describes the concept of ADTS and illustrates the concept with three o.docx
 
Data structure Assignment Help
Data structure Assignment HelpData structure Assignment Help
Data structure Assignment Help
 
Data structures
Data structuresData structures
Data structures
 
java.pdf
java.pdfjava.pdf
java.pdf
 
linked list.pptx
linked list.pptxlinked list.pptx
linked list.pptx
 
3.ppt
3.ppt3.ppt
3.ppt
 
3.ppt
3.ppt3.ppt
3.ppt
 
Data structure,abstraction,abstract data type,static and dynamic,time and spa...
Data structure,abstraction,abstract data type,static and dynamic,time and spa...Data structure,abstraction,abstract data type,static and dynamic,time and spa...
Data structure,abstraction,abstract data type,static and dynamic,time and spa...
 
Data Structures & Algorithms Unit 1.pptx
Data Structures & Algorithms Unit 1.pptxData Structures & Algorithms Unit 1.pptx
Data Structures & Algorithms Unit 1.pptx
 

Mehr von Deepam Aggarwal

Frames and its components
Frames and its components Frames and its components
Frames and its components Deepam Aggarwal
 
Monolithic and Procedural Programming
Monolithic and Procedural ProgrammingMonolithic and Procedural Programming
Monolithic and Procedural ProgrammingDeepam Aggarwal
 
Group By, Having Clause and Order By clause
Group By, Having Clause and Order By clause Group By, Having Clause and Order By clause
Group By, Having Clause and Order By clause Deepam Aggarwal
 
Cyber Criminal and Cyber Security
Cyber Criminal and Cyber Security Cyber Criminal and Cyber Security
Cyber Criminal and Cyber Security Deepam Aggarwal
 
Powersharinginindia 150527161221-lva1-app6892
Powersharinginindia 150527161221-lva1-app6892Powersharinginindia 150527161221-lva1-app6892
Powersharinginindia 150527161221-lva1-app6892Deepam Aggarwal
 
Triangle and its properties
Triangle and its propertiesTriangle and its properties
Triangle and its propertiesDeepam Aggarwal
 
probability-game of chances
probability-game of chancesprobability-game of chances
probability-game of chancesDeepam Aggarwal
 
Political and public awareness through media (B.st)
Political and public awareness through media (B.st)Political and public awareness through media (B.st)
Political and public awareness through media (B.st)Deepam Aggarwal
 

Mehr von Deepam Aggarwal (13)

Frames and its components
Frames and its components Frames and its components
Frames and its components
 
Bank managment system
Bank managment systemBank managment system
Bank managment system
 
Monolithic and Procedural Programming
Monolithic and Procedural ProgrammingMonolithic and Procedural Programming
Monolithic and Procedural Programming
 
Inventory Management
Inventory Management Inventory Management
Inventory Management
 
Group By, Having Clause and Order By clause
Group By, Having Clause and Order By clause Group By, Having Clause and Order By clause
Group By, Having Clause and Order By clause
 
Cyber Criminal and Cyber Security
Cyber Criminal and Cyber Security Cyber Criminal and Cyber Security
Cyber Criminal and Cyber Security
 
Mdi vb.net
Mdi vb.netMdi vb.net
Mdi vb.net
 
Stress Management
Stress Management Stress Management
Stress Management
 
Impressionism
ImpressionismImpressionism
Impressionism
 
Powersharinginindia 150527161221-lva1-app6892
Powersharinginindia 150527161221-lva1-app6892Powersharinginindia 150527161221-lva1-app6892
Powersharinginindia 150527161221-lva1-app6892
 
Triangle and its properties
Triangle and its propertiesTriangle and its properties
Triangle and its properties
 
probability-game of chances
probability-game of chancesprobability-game of chances
probability-game of chances
 
Political and public awareness through media (B.st)
Political and public awareness through media (B.st)Political and public awareness through media (B.st)
Political and public awareness through media (B.st)
 

Kürzlich hochgeladen

The Contemporary World: The Globalization of World Politics
The Contemporary World: The Globalization of World PoliticsThe Contemporary World: The Globalization of World Politics
The Contemporary World: The Globalization of World PoliticsRommel Regala
 
Oppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmOppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmStan Meyer
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operationalssuser3e220a
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfPatidar M
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Seán Kennedy
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
Dust Of Snow By Robert Frost Class-X English CBSE
Dust Of Snow By Robert Frost Class-X English CBSEDust Of Snow By Robert Frost Class-X English CBSE
Dust Of Snow By Robert Frost Class-X English CBSEaurabinda banchhor
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSJoshuaGantuangco2
 
Textual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSTextual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSMae Pangan
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxlancelewisportillo
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
Presentation Activity 2. Unit 3 transv.pptx
Presentation Activity 2. Unit 3 transv.pptxPresentation Activity 2. Unit 3 transv.pptx
Presentation Activity 2. Unit 3 transv.pptxRosabel UA
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 

Kürzlich hochgeladen (20)

The Contemporary World: The Globalization of World Politics
The Contemporary World: The Globalization of World PoliticsThe Contemporary World: The Globalization of World Politics
The Contemporary World: The Globalization of World Politics
 
Oppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmOppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and Film
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operational
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdf
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...
 
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptxLEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
 
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
Dust Of Snow By Robert Frost Class-X English CBSE
Dust Of Snow By Robert Frost Class-X English CBSEDust Of Snow By Robert Frost Class-X English CBSE
Dust Of Snow By Robert Frost Class-X English CBSE
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
 
Textual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSTextual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHS
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
Presentation Activity 2. Unit 3 transv.pptx
Presentation Activity 2. Unit 3 transv.pptxPresentation Activity 2. Unit 3 transv.pptx
Presentation Activity 2. Unit 3 transv.pptx
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 

Dynamic memory allocation and linked lists

  • 2. Dynamic memory location: The process of allocating memory at run time is known as dynamic memory allocation. C does not inherently have this facility. There are four library routines known as memory management functions that can be used for allocating and freeing memory during execution. malloc(), calloc(), free(), realloc() malloc: Allocating a block of memory General form ptr = (cast-type *) malloc(byte-size); ptr is a pointer of type cast-type The malloc() returns a pointer (of cast-type) to an area of memory with size byte- size. If there is not enough space a NULL pointer is returned.
  • 3. Example x=(int *) malloc(100*sizeof(int)); cptr=(char *) malloc(10); st= (struct *) malloc(sizeof(struct store)); Storage space allocated dynamically has no name and therefore its contents can be accessed only through a pointer. calloc: Allocating multiple block of memory While malloc() allocates a single block of storage space, calloc() allocates multiple blocks of storage, each of the same size, and then set all bytes to zero. If there is not enough space a NULL pointer is returned. General form ptr=(cast-type *) calloc(n, element-size); free( ); Releasing the used space With dynamic run-time allocation, we need to release the space when it is not required free(ptr); Frees previously allocated space created by malloc() or calloc()
  • 4. realloc( ); Altering the size of a block It is likely that the previous allocated memory is not sufficient and we need additional space for more elements. It is also possible that the memory allocated is much larger than necessary and we want to reduce it. ptr=realloc(ptr, newsize); Modifies the size of previously allocated space by malloc() or calloc(). Linked List A list refers to a set of items organized sequentially Linked list is a completely different way to represent a list is to make each item in the list part of a structure that also contains a “link” to the structure containing the next item. A linked list is a dynamic data structures.
  • 5. Dynamic Memory Allocation Basic It is a consistent set of a fixed number of data items. It is an ordered set comprising a variable number of data items. Size Specified during declaration. No need to specify; grow and shrink during execution Storage Allocation Element location is allocated during compile time. Element position is assigned during run time. Order of the elements Stored consecutively Stored randomly Accessing the element Direct or randomly accessed, i.e., Specify the array index or subscript. Sequentially accessed, i.e., Traverse starting from the first node in the list by the pointer. Difference between array and linked list Array Linked List
  • 6. Insertion and deletion of element Slow relatively as shifting is required. Easier, fast and efficient. Searching Binary search and linear search linear search Memory required less More Memory Utilization Ineffective Efficient Difference between array and linked list Array Linked List
  • 7. Advantages of linked list • Can grow or shrink in size during the execution of a program • Does not waste memory space • Here it is not necessary to specify the number of nodes to be used in the list • It provides flexibility is allowing the items to be rearranged efficiently • It is easier to insert or delete items by rearranging the links Limitation of Linked List • The access to any arbitrary item is little cumbersome and time consuming. • Use more storage than an array with the same number of items. This is because each item has an additional link field. • Linked lists are traversed in unidirection • Complex to implement • Sequential access to elements • Leads to memory problems if not taken care about the pointer manipulations properly That is, Whenever we deal with a fixed length list, it would be better to use an array rather than a linked list.
  • 8. What we can do with Linked List:- 1. Creating a list. 2. Traversing the list. 3. Counting the items in the list. 4. Printing the list (or sub list). 5. Looking up an item for editing or printing. 6. Inserting an item. 7. Deleting an item. 8. Concatenating two lists. 9. Replace() : replaces a value stored at a position with some other value. 10.Swap() : swap the values of the nodes specified by two positions. 11. Merging two linked lists into a larger list. 12.Searching for an element in a linked list. 13.Reversing a linked list.