Destructor CSCE 121.

Slides:



Advertisements
Similar presentations
Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Advertisements

Dynamic Memory Allocation (also see pointers lectures) -L. Grewe.
Introduction to Programming Lecture 39. Copy Constructor.
CS-1030 Dr. Mark L. Hornick 1 Pointers And Dynamic Memory.
Destructors Math 130 Lecture # xx Mo/Da/Yr B Smith: New lecture for 05. Use this for evolving to Java B Smith: New lecture for 05. Use this for evolving.
Dynamic Memory Allocation in C++. Memory Segments in C++ Memory is divided in certain segments – Code Segment Stores application code – Data Segment Holds.
CSE 2501 Review Declaring a variable allocates space for the type of datum it is to store int x; // allocates space for an int int *px; // allocates space.
Pointers and Dynamic Variables. Objectives on completion of this topic, students should be able to: Correctly allocate data dynamically * Use the new.
CS 225 Lab #2 - Pointers, Copy Constructors, Destructors, and DDD.
CS 11 C++ track: lecture 4 Today: More on memory management the stack and the heap inline functions structs vs. classes.
C++ Memory Overview 4 major memory segments Key differences from Java
 Managing the heap  Resource acquisition is initialization (RAII)  Overriding operator new and delete  Class-based memory pools.
Object-Oriented Programming in C++
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
1 Review: C++ class 2 kinds of class members: data members and function members Class members are private by default Data members are generally private.
Dynamic Memory Review l what is static, automatic, dynamic variables? Why are dynamic(ally allocated) variables needed l what is program stack? Function.
POINTERS AND MEMORY ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED BY NANCY M. AMATO AND JORY DENNY 1.
Pointers and References. Pointers & Memory 0x000x040x080x0B0x100x140x180x1B0x20.
Memory Management.
Constructors and Destructors
CMSC 341 Lecture 2 – Dynamic Memory and Pointers (Review)
Memory Management with Classes
Overview 4 major memory segments Key differences from Java stack
CS 215 Final Review Ismail abumuhfouz Fall 2014.
Programming with ANSI C ++
C++ Object-Oriented Programming
Constructor & Destructor
Dynamic Memory CSCE 121 J. Michael Moore.
Chapter 10: Pointers Starting Out with C++ Early Objects Ninth Edition
Memberwise Assignment / Initialization
Dynamic Memory Review what is static, automatic, dynamic variables? why are dynamic(ally allocated) variables needed what is program stack? function.
Dynamically Allocated Memory
Automatics, Copy Constructor, and Assignment Operator
Overview 4 major memory segments Key differences from Java stack
understanding memory usage by a c++ program
Chapter 15 Pointers, Dynamic Data, and Reference Types
Automatics, Copy Constructor, and Assignment Operator
Overview of Memory Layout in C++
Object Oriented Programming (OOP) Lecture No. 9
Dynamic Memory A whole heap of fun….
Dynamic Memory Copy Challenge
Chapter 15 Pointers, Dynamic Data, and Reference Types
Pointers And Memory Acknowledgement: THE Slides are Prepared FROM SLIDES PROVIDED By NANCY M. AMATO AND Jory Denny.
Constructors and Destructors
Destruction and Copying
Indirection.
Summary: Abstract Data Type
How Dynamic Memory Works with Memory Diagram
Dynamic Memory Management
Essential Class Operations
Destructor CSCE 121 J. Michael Moore.
Objects Managing a Resource
Introduction to Classes and Objects
Dynamic Memory A whole heap of fun….
Destruction and Copying
Dynamic Memory And Objects
COP 3330 Object-oriented Programming in C++
Dynamic Memory Copy Challenge
Review: C++ class represents an ADT
Data Structures and Algorithms Memory allocation and Dynamic Array
Class: Special Topics 2 For classes using memory allocation
Pointers and References
Essential Class Operations
How Dynamic Memory Works with Memory Diagram
Dynamic Memory CSCE 121.
How Memory Leaks Work with Memory Diagram
Dynamic Memory Management
Rule of Three Part 1 & 2.
SPL – PS3 C++ Classes.
SPL – PS2 C++ Memory Handling.
Presentation transcript:

Destructor CSCE 121

Destructor System automatically frees variables in a class when it is finished Kind of like a built in destructor, but not a good one if you use dynamic memory (i.e. the heap) Can be called explicitly (but rarely this way) If you want to know how, look it up! Normally called implicitly When an object goes out of scope on the stack When delete is called on a pointer to an object on the heap

Destruction Process (Automatic) Object’s destructor function called Destructors called for each data member that is a class. Note pointers to a class are NOT a class; they are memory addresses. Destructors called for each base class. Basic data types are freed with the object. These include int, char, double, and any pointers. If a pointer refers to dynamic memory, the heap memory will be left behind. Memory Leak!!! This will make sense after we talk about inheritance!

Destructor Responsibility Free any dynamic memory before the object’s memory is freed. In other words, call delete for any time new was called during the lifetime of the object. This is freeing resources in RAII.

Anatomy Destructor uses the Class name pre-pended by a tilde (~) No parameters allowed. If you have to write one, then you are probably using ‘delete’ in it. Class LinkedList { LinkedList(); // constructor ~LinkedList(); // destructor }