SlideShare una empresa de Scribd logo
1 de 32
C++ Memory Management Anil Bapat
Agenda ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Common Pointer related pitfalls ,[object Object],[object Object],[object Object],[object Object]
Common Pointer related pitfalls (Contd) ,[object Object],[object Object],[object Object]
Common Pointer related pitfalls (Contd) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
new/delete operators ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
new/delete operators (Contd) ,[object Object],[object Object]
new/delete operators (Contd) ,[object Object],[object Object]
new/delete operators (Contd) ,[object Object],[object Object],[object Object],[object Object],[object Object]
placement new/delete ,[object Object],[object Object],[object Object]
the new_handler ,[object Object],[object Object],[object Object]
Overloading new/delete operators ,[object Object],[object Object],[object Object]
Overloading new/delete operators (Contd) ,[object Object],[object Object]
Overloading new/delete operators (Contd) ,[object Object],[object Object]
Why replace new/delete? ,[object Object],[object Object],[object Object]
Performance ,[object Object],[object Object],[object Object],[object Object]
Debugging ,[object Object],[object Object]
Collecting statistics ,[object Object],[object Object]
Memory pools ,[object Object],[object Object],[object Object],[object Object],[object Object]
Class Specific Pools ,[object Object],[object Object],[object Object]
Memory debuggers are tricked ,[object Object],[object Object],[object Object],[object Object]
Smart Pointers ,[object Object],[object Object],[object Object]
The ‘Smart’ part ,[object Object],[object Object],[object Object]
Smarter, and invisible too! ,[object Object],[object Object],[object Object]
Smart pointer class template <class T> class SmartPointer { public: SmartPointer(T* pointee = NULL) : _pointee(pointee); SmartPointer(const SmartPointer& rhs); SmartPointer& operator=(const SmartPointer& rhs); ~SmartPointer(); T* operator->() const; T& operator*() const; bool operator!() const; template <class Destination> operator SmartPointer<Destination>() { return SmartPointer<Destination>(_pointee);}; private: T* _pointee; };
Smart pointer usage Base* pBase = new Base; Base* pDerived = new Derived; extern func(SmartPointer<Base> spBase); // Function that takes  // base smart pointer as argument SmartPointer<Derived> sp(pDerived); sp->mData = 0; cout << (*sp).mData; if (!sp) cout << “pClass pointer value is NULL”; func(sp) // Can pass smart pointer of derived class type to a  // function that accepts smart pointer of base class type
Types of smart pointers ,[object Object],[object Object],[object Object],[object Object],[object Object]
Scoped Pointers ,[object Object],[object Object],[object Object]
Reference counting ,[object Object],[object Object],[object Object]
Reference counting (Contd) ,[object Object],[object Object],[object Object],[object Object],[object Object]
Copy on Write ,[object Object],[object Object],[object Object],[object Object]
Smart pointer techniques beyond pointers ,[object Object],[object Object],[object Object]

Más contenido relacionado

La actualidad más candente

Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
Abhilash Nair
 
Java - Exception Handling
Java - Exception HandlingJava - Exception Handling
Java - Exception Handling
Prabhdeep Singh
 

La actualidad más candente (20)

Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
Java Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overridingJava Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overriding
 
Function overloading ppt
Function overloading pptFunction overloading ppt
Function overloading ppt
 
16 virtual function
16 virtual function16 virtual function
16 virtual function
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
 
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member Functions
 
Data types in java
Data types in javaData types in java
Data types in java
 
Constructors and Destructor in C++
Constructors and Destructor in C++Constructors and Destructor in C++
Constructors and Destructor in C++
 
Vectors in Java
Vectors in JavaVectors in Java
Vectors in Java
 
Function overloading(c++)
Function overloading(c++)Function overloading(c++)
Function overloading(c++)
 
Oops concept on c#
Oops concept on c#Oops concept on c#
Oops concept on c#
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Primitive data types in java
Primitive data types in javaPrimitive data types in java
Primitive data types in java
 
Java - Exception Handling
Java - Exception HandlingJava - Exception Handling
Java - Exception Handling
 
Standard template library
Standard template libraryStandard template library
Standard template library
 
Java Data Types
Java Data TypesJava Data Types
Java Data Types
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Class 3 - PHP Functions
Class 3 - PHP FunctionsClass 3 - PHP Functions
Class 3 - PHP Functions
 
Strings in Java
Strings in JavaStrings in Java
Strings in Java
 

Destacado

Unix memory management
Unix memory managementUnix memory management
Unix memory management
Tech_MX
 
Virtual memory
Virtual memoryVirtual memory
Virtual memory
Anuj Modi
 

Destacado (13)

Unix memory management
Unix memory managementUnix memory management
Unix memory management
 
Virtual memory
Virtual memoryVirtual memory
Virtual memory
 
VIRTUAL MEMORY
VIRTUAL MEMORYVIRTUAL MEMORY
VIRTUAL MEMORY
 
Virtual Memory and Paging
Virtual Memory and PagingVirtual Memory and Paging
Virtual Memory and Paging
 
Os Swapping, Paging, Segmentation and Virtual Memory
Os Swapping, Paging, Segmentation and Virtual MemoryOs Swapping, Paging, Segmentation and Virtual Memory
Os Swapping, Paging, Segmentation and Virtual Memory
 
Virtual memory ppt
Virtual memory pptVirtual memory ppt
Virtual memory ppt
 
8 memory management strategies
8 memory management strategies8 memory management strategies
8 memory management strategies
 
Chapter 9 - Virtual Memory
Chapter 9 - Virtual MemoryChapter 9 - Virtual Memory
Chapter 9 - Virtual Memory
 
Memory management
Memory managementMemory management
Memory management
 
Memory management
Memory managementMemory management
Memory management
 
Paging and Segmentation in Operating System
Paging and Segmentation in Operating SystemPaging and Segmentation in Operating System
Paging and Segmentation in Operating System
 
Memory management
Memory managementMemory management
Memory management
 
Operating Systems and Memory Management
Operating Systems and Memory ManagementOperating Systems and Memory Management
Operating Systems and Memory Management
 

Similar a C++ Memory Management

New microsoft office word document (2)
New microsoft office word document (2)New microsoft office word document (2)
New microsoft office word document (2)
rashmita_mishra
 
CS225_Prelecture_Notes 2nd
CS225_Prelecture_Notes 2ndCS225_Prelecture_Notes 2nd
CS225_Prelecture_Notes 2nd
Edward Chen
 
Process Synchronization Producer-Consumer ProblemThe purpos.docx
Process Synchronization Producer-Consumer ProblemThe purpos.docxProcess Synchronization Producer-Consumer ProblemThe purpos.docx
Process Synchronization Producer-Consumer ProblemThe purpos.docx
stilliegeorgiana
 

Similar a C++ Memory Management (20)

PPT DMA.pptx
PPT  DMA.pptxPPT  DMA.pptx
PPT DMA.pptx
 
New microsoft office word document (2)
New microsoft office word document (2)New microsoft office word document (2)
New microsoft office word document (2)
 
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
 
C questions
C questionsC questions
C questions
 
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
 
CS225_Prelecture_Notes 2nd
CS225_Prelecture_Notes 2ndCS225_Prelecture_Notes 2nd
CS225_Prelecture_Notes 2nd
 
Data structures
Data structuresData structures
Data structures
 
Dynamic Memory Allocation, Pointers and Functions, Pointers and Structures
Dynamic Memory Allocation, Pointers and Functions, Pointers and StructuresDynamic Memory Allocation, Pointers and Functions, Pointers and Structures
Dynamic Memory Allocation, Pointers and Functions, Pointers and Structures
 
C++.pptx
C++.pptxC++.pptx
C++.pptx
 
One Careful Owner
One Careful OwnerOne Careful Owner
One Careful Owner
 
C++ Training
C++ TrainingC++ Training
C++ Training
 
Java performance
Java performanceJava performance
Java performance
 
memory
memorymemory
memory
 
ADBMS ASSIGNMENT
ADBMS ASSIGNMENTADBMS ASSIGNMENT
ADBMS ASSIGNMENT
 
Unit3_OOP-converted.pdf
Unit3_OOP-converted.pdfUnit3_OOP-converted.pdf
Unit3_OOP-converted.pdf
 
C++ Interview Questions
C++ Interview QuestionsC++ Interview Questions
C++ Interview Questions
 
Process Synchronization Producer-Consumer ProblemThe purpos.docx
Process Synchronization Producer-Consumer ProblemThe purpos.docxProcess Synchronization Producer-Consumer ProblemThe purpos.docx
Process Synchronization Producer-Consumer ProblemThe purpos.docx
 
Smart pointers
Smart pointersSmart pointers
Smart pointers
 
1183 c-interview-questions-and-answers
1183 c-interview-questions-and-answers1183 c-interview-questions-and-answers
1183 c-interview-questions-and-answers
 

Último

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 

Último (20)

Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 

C++ Memory Management

  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25. Smart pointer class template <class T> class SmartPointer { public: SmartPointer(T* pointee = NULL) : _pointee(pointee); SmartPointer(const SmartPointer& rhs); SmartPointer& operator=(const SmartPointer& rhs); ~SmartPointer(); T* operator->() const; T& operator*() const; bool operator!() const; template <class Destination> operator SmartPointer<Destination>() { return SmartPointer<Destination>(_pointee);}; private: T* _pointee; };
  • 26. Smart pointer usage Base* pBase = new Base; Base* pDerived = new Derived; extern func(SmartPointer<Base> spBase); // Function that takes // base smart pointer as argument SmartPointer<Derived> sp(pDerived); sp->mData = 0; cout << (*sp).mData; if (!sp) cout << “pClass pointer value is NULL”; func(sp) // Can pass smart pointer of derived class type to a // function that accepts smart pointer of base class type
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.