SlideShare a Scribd company logo
1 of 10
 There are essentially two types of memory allocation
Static – Done by the compiler automatically (implicitly).
Global variables or objects -- memory is allocated at the start of
the program, and freed when program exits; alive throughout
program execution
 Can be access anywhere in the program.
Local variables (inside a routine) – memory is allocated when the
routine starts and freed when the routine returns.
 A local variable cannot be accessed from another routine.
Allocation and free are done implicitly.
No need to explicitly manage memory is nice (easy to work with),
but has limitations!
Using static allocation, the array size must be fixed.
Consider the grade roster for the class? What is the number of people
in the class?
 There are essentially two types of memory
allocation
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.
 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
 Examples:
◦ char *my_char_ptr = new char;
◦ int *my_int_array =new int[20];
◦ Mixed *m1 = new Mixed(1,1,2);
◦ Before the assignment, the pointer may or may not point to a
legitimate memory
◦ After the assignment, the pointer points to a legitimate memory.
sample1.cpp
sample2.cpp
 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.
sample3.cpp
(linprog and diablo)
sample4.cpp
 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)
sample5.cpp
 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
sample6.cpp
sample7_1.cpp
sample7.cpp
 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->Simplifly() is equivelant
to(*(M1_ptr)).Simplify()
sample8.cpp
 The malloc and free routines
◦ Prototype defined in stdlib.h
◦ malloc is similar to new except that it specifies the exact
memory size
 Return a (void *) -- needs to convert to the right pointer type
◦ free is equivalent to delete (only one form for both single
item and many items).
Sample3_malloc.cpp
 The malloc and free routines
◦ Prototype defined in stdlib.h
◦ malloc is similar to new except that it specifies the exact
memory size
 Return a (void *) -- needs to convert to the right pointer type
◦ free is equivalent to delete (only one form for both single
item and many items).
Sample3_malloc.cpp

More Related Content

What's hot

Dynamic memory Allocation in c language
Dynamic memory Allocation in c languageDynamic memory Allocation in c language
Dynamic memory Allocation in c languagekiran Patel
 
16 dynamic-memory-allocation
16 dynamic-memory-allocation16 dynamic-memory-allocation
16 dynamic-memory-allocationRohit Shrivastava
 
Memory allocation in c
Memory allocation in cMemory allocation in c
Memory allocation in cPrabhu Govind
 
Dynamic Memory Allocation
Dynamic Memory AllocationDynamic Memory Allocation
Dynamic Memory Allocationvaani pathak
 
Memory management
Memory managementMemory management
Memory managementsana younas
 
Dynamic Memory Allocation(DMA)
Dynamic Memory Allocation(DMA)Dynamic Memory Allocation(DMA)
Dynamic Memory Allocation(DMA)Kamal Acharya
 
storage clas
storage classtorage clas
storage clasteach4uin
 
Applications of data structures
Applications of data structuresApplications of data structures
Applications of data structuresWipro
 
Data structure and algorithm
Data structure and algorithmData structure and algorithm
Data structure and algorithmTrupti Agrawal
 
Python advanced 3.the python std lib by example –data structures
Python advanced 3.the python std lib by example –data structuresPython advanced 3.the python std lib by example –data structures
Python advanced 3.the python std lib by example –data structuresJohn(Qiang) Zhang
 
Linked list using Dynamic Memory Allocation
Linked list using Dynamic Memory AllocationLinked list using Dynamic Memory Allocation
Linked list using Dynamic Memory Allocationkiran Patel
 
Cupdf.com introduction to-data-structures-and-algorithm
Cupdf.com introduction to-data-structures-and-algorithmCupdf.com introduction to-data-structures-and-algorithm
Cupdf.com introduction to-data-structures-and-algorithmTarikuDabala1
 
Dynamic Memory & Linked Lists
Dynamic Memory & Linked ListsDynamic Memory & Linked Lists
Dynamic Memory & Linked ListsAfaq Mansoor Khan
 
Task and Data Parallelism: Real-World Examples
Task and Data Parallelism: Real-World ExamplesTask and Data Parallelism: Real-World Examples
Task and Data Parallelism: Real-World ExamplesSasha Goldshtein
 

What's hot (20)

Stack and Heap
Stack and HeapStack and Heap
Stack and Heap
 
Dynamic memory Allocation in c language
Dynamic memory Allocation in c languageDynamic memory Allocation in c language
Dynamic memory Allocation in c language
 
16 dynamic-memory-allocation
16 dynamic-memory-allocation16 dynamic-memory-allocation
16 dynamic-memory-allocation
 
Memory allocation in c
Memory allocation in cMemory allocation in c
Memory allocation in c
 
Dynamic Memory Allocation
Dynamic Memory AllocationDynamic Memory Allocation
Dynamic Memory Allocation
 
Memory management
Memory managementMemory management
Memory management
 
Dynamic Memory Allocation(DMA)
Dynamic Memory Allocation(DMA)Dynamic Memory Allocation(DMA)
Dynamic Memory Allocation(DMA)
 
storage clas
storage classtorage clas
storage clas
 
Applications of data structures
Applications of data structuresApplications of data structures
Applications of data structures
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocation
 
Data structure and algorithm
Data structure and algorithmData structure and algorithm
Data structure and algorithm
 
Python advanced 3.the python std lib by example –data structures
Python advanced 3.the python std lib by example –data structuresPython advanced 3.the python std lib by example –data structures
Python advanced 3.the python std lib by example –data structures
 
Linked list using Dynamic Memory Allocation
Linked list using Dynamic Memory AllocationLinked list using Dynamic Memory Allocation
Linked list using Dynamic Memory Allocation
 
Cupdf.com introduction to-data-structures-and-algorithm
Cupdf.com introduction to-data-structures-and-algorithmCupdf.com introduction to-data-structures-and-algorithm
Cupdf.com introduction to-data-structures-and-algorithm
 
Python algorithm
Python algorithmPython algorithm
Python algorithm
 
Program activation records
Program activation recordsProgram activation records
Program activation records
 
Dynamic Memory & Linked Lists
Dynamic Memory & Linked ListsDynamic Memory & Linked Lists
Dynamic Memory & Linked Lists
 
Python recursion
Python recursionPython recursion
Python recursion
 
Pointer
PointerPointer
Pointer
 
Task and Data Parallelism: Real-World Examples
Task and Data Parallelism: Real-World ExamplesTask and Data Parallelism: Real-World Examples
Task and Data Parallelism: Real-World Examples
 

Similar to memory (20)

PPT DMA.pptx
PPT  DMA.pptxPPT  DMA.pptx
PPT DMA.pptx
 
Memory Allocation & Direct Memory Allocation in C & C++ Language PPT
Memory Allocation & Direct Memory Allocation in C & C++ Language PPTMemory Allocation & Direct Memory Allocation in C & C++ Language PPT
Memory Allocation & Direct Memory Allocation in C & C++ Language PPT
 
ADK COLEGE.pptx
ADK COLEGE.pptxADK COLEGE.pptx
ADK COLEGE.pptx
 
16829 memory management2
16829 memory management216829 memory management2
16829 memory management2
 
Unit 5
Unit 5Unit 5
Unit 5
 
C++ QNA
C++ QNAC++ QNA
C++ QNA
 
16858 memory management2
16858 memory management216858 memory management2
16858 memory management2
 
C basic questions&ansrs by shiva kumar kella
C basic questions&ansrs by shiva kumar kellaC basic questions&ansrs by shiva kumar kella
C basic questions&ansrs by shiva kumar kella
 
Dma
DmaDma
Dma
 
Devry gsp 215 week 6 i lab virtual memory new
Devry gsp 215 week 6 i lab virtual memory newDevry gsp 215 week 6 i lab virtual memory new
Devry gsp 215 week 6 i lab virtual memory new
 
Programming in C sesion 2
Programming in C sesion 2Programming in C sesion 2
Programming in C sesion 2
 
Dynamic Memory Allocation in C
Dynamic Memory Allocation in CDynamic Memory Allocation in C
Dynamic Memory Allocation in C
 
VCE Unit 01 (1).pptx
VCE Unit 01 (1).pptxVCE Unit 01 (1).pptx
VCE Unit 01 (1).pptx
 
Analytics tools and Instruments
Analytics tools and InstrumentsAnalytics tools and Instruments
Analytics tools and Instruments
 
Stack and heap
Stack and heapStack and heap
Stack and heap
 
C, C++ Interview Questions Part - 1
C, C++ Interview Questions Part - 1C, C++ Interview Questions Part - 1
C, C++ Interview Questions Part - 1
 
CS225_Prelecture_Notes 2nd
CS225_Prelecture_Notes 2ndCS225_Prelecture_Notes 2nd
CS225_Prelecture_Notes 2nd
 
Unit 1
Unit  1Unit  1
Unit 1
 
Storage class
Storage classStorage class
Storage class
 
Storage class
Storage classStorage class
Storage class
 

More from teach4uin

Master pages
Master pagesMaster pages
Master pagesteach4uin
 
.Net framework
.Net framework.Net framework
.Net frameworkteach4uin
 
Scripting languages
Scripting languagesScripting languages
Scripting languagesteach4uin
 
State management
State managementState management
State managementteach4uin
 
security configuration
security configurationsecurity configuration
security configurationteach4uin
 
static dynamic html tags
 static dynamic html tags static dynamic html tags
static dynamic html tagsteach4uin
 
static dynamic html tags
static dynamic html tagsstatic dynamic html tags
static dynamic html tagsteach4uin
 
New microsoft office power point presentation
New microsoft office power point presentationNew microsoft office power point presentation
New microsoft office power point presentationteach4uin
 
.Net overview
.Net overview.Net overview
.Net overviewteach4uin
 
Stdlib functions lesson
Stdlib functions lessonStdlib functions lesson
Stdlib functions lessonteach4uin
 
Cprogrammingprogramcontrols
CprogrammingprogramcontrolsCprogrammingprogramcontrols
Cprogrammingprogramcontrolsteach4uin
 
Cprogrammingoperator
CprogrammingoperatorCprogrammingoperator
Cprogrammingoperatorteach4uin
 

More from teach4uin (20)

Controls
ControlsControls
Controls
 
validation
validationvalidation
validation
 
validation
validationvalidation
validation
 
Master pages
Master pagesMaster pages
Master pages
 
.Net framework
.Net framework.Net framework
.Net framework
 
Scripting languages
Scripting languagesScripting languages
Scripting languages
 
Css1
Css1Css1
Css1
 
Code model
Code modelCode model
Code model
 
Asp db
Asp dbAsp db
Asp db
 
State management
State managementState management
State management
 
security configuration
security configurationsecurity configuration
security configuration
 
static dynamic html tags
 static dynamic html tags static dynamic html tags
static dynamic html tags
 
static dynamic html tags
static dynamic html tagsstatic dynamic html tags
static dynamic html tags
 
New microsoft office power point presentation
New microsoft office power point presentationNew microsoft office power point presentation
New microsoft office power point presentation
 
.Net overview
.Net overview.Net overview
.Net overview
 
Stdlib functions lesson
Stdlib functions lessonStdlib functions lesson
Stdlib functions lesson
 
enums
enumsenums
enums
 
array
arrayarray
array
 
Cprogrammingprogramcontrols
CprogrammingprogramcontrolsCprogrammingprogramcontrols
Cprogrammingprogramcontrols
 
Cprogrammingoperator
CprogrammingoperatorCprogrammingoperator
Cprogrammingoperator
 

Recently uploaded

Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
Bluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfBluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfngoud9212
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraDeakin University
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Neo4j
 

Recently uploaded (20)

Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
Bluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfBluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdf
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning era
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024
 

memory

  • 1.
  • 2.  There are essentially two types of memory allocation Static – Done by the compiler automatically (implicitly). Global variables or objects -- memory is allocated at the start of the program, and freed when program exits; alive throughout program execution  Can be access anywhere in the program. Local variables (inside a routine) – memory is allocated when the routine starts and freed when the routine returns.  A local variable cannot be accessed from another routine. Allocation and free are done implicitly. No need to explicitly manage memory is nice (easy to work with), but has limitations! Using static allocation, the array size must be fixed. Consider the grade roster for the class? What is the number of people in the class?
  • 3.  There are essentially two types of memory allocation 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.
  • 4.  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  Examples: ◦ char *my_char_ptr = new char; ◦ int *my_int_array =new int[20]; ◦ Mixed *m1 = new Mixed(1,1,2); ◦ Before the assignment, the pointer may or may not point to a legitimate memory ◦ After the assignment, the pointer points to a legitimate memory. sample1.cpp sample2.cpp
  • 5.  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. sample3.cpp (linprog and diablo) sample4.cpp
  • 6.  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) sample5.cpp
  • 7.  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 sample6.cpp sample7_1.cpp sample7.cpp
  • 8.  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->Simplifly() is equivelant to(*(M1_ptr)).Simplify() sample8.cpp
  • 9.  The malloc and free routines ◦ Prototype defined in stdlib.h ◦ malloc is similar to new except that it specifies the exact memory size  Return a (void *) -- needs to convert to the right pointer type ◦ free is equivalent to delete (only one form for both single item and many items). Sample3_malloc.cpp
  • 10.  The malloc and free routines ◦ Prototype defined in stdlib.h ◦ malloc is similar to new except that it specifies the exact memory size  Return a (void *) -- needs to convert to the right pointer type ◦ free is equivalent to delete (only one form for both single item and many items). Sample3_malloc.cpp