CS-1030 Dr. Mark L. Hornick 1 Pointers And Dynamic Memory.

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

Chapter 6 Data Types
Dynamic Memory Allocation (also see pointers lectures) -L. Grewe.
CS-1030 Dr. Mark L. Hornick 1 Constructors Copy Constructors.
Various languages….  Could affect performance  Could affect reliability  Could affect language choice.
Classes and Objects: Chapter 8, Slide 1 Object and its encapsulation.
Memory allocation CSE 2451 Matt Boggus. sizeof The sizeof unary operator will return the number of bytes reserved for a variable or data type. Determine:
Pointers Revisited l What is variable address, name, value? l What is a pointer? l How is a pointer declared? l What is address-of (reference) and dereference.
Informática II Prof. Dr. Gustavo Patiño MJ
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 18.
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
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.
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
Dynamically Allocated Arrays May 2, Quiz 5 Today.
Pointers and Dynamic Variables. Objectives on completion of this topic, students should be able to: Correctly allocate data dynamically * Use the new.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
Memory and C++ Pointers.  C++ objects and memory  C++ primitive types and memory  Note: “primitive types” = int, long, float, double, char, … January.
CS 225 Lab #2 - Pointers, Copy Constructors, Destructors, and DDD.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
Pointer Data Type and Pointer Variables
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
1 Data Structures - CSCI 102 CS102 C++ Pointers & Dynamic Objects Prof Tejada.
C++ Memory Overview 4 major memory segments Key differences from Java
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
Chapter 9 Pointers and Dynamic Arrays (9.1). Pointers A variables which holds the memory address for a variable of a specific type. Call-by-Reference.
CPSC 252 Dynamic Memory Allocation Page 1 Dynamic memory allocation Our first IntVector class has some serious limitations the capacity is fixed at MAX_SIZE.
1 Dynamic Memory Allocation –The need –malloc/free –Memory Leaks –Dangling Pointers and Garbage Collection Today’s Material.
 Managing the heap  Resource acquisition is initialization (RAII)  Overriding operator new and delete  Class-based memory pools.
Dynamic Memory Allocation. Domain A subset of the total domain name space. A domain represents a level of the hierarchy in the Domain Name Space, and.
Object-Oriented Programming in C++
Dynamic memory allocation and Pointers Lecture 4.
Pointers in C++. 7a-2 Pointers "pointer" is a basic type like int or double value of a pointer variable contains the location, or address in memory, of.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
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.
Objects and Variables Local variables – Confined to single context: allocated on stack – Primitive types such as int or object references – Must be initialized.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Smart Pointers. Dumb Pointers Pointers Necessary – Dynamic memory – Sharing memory.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 4 is due Nov. 20 (next Friday). SDD document framework should be set up in your Wiki by now.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 3 is due Sunday, the 8 th at 7pm. Today: –Two simple binding examples. –Function Hiding.
CS-1030 Dr. Mark L. Hornick 1 References & Pointers.
Dynamic Memory Management & Static Class Members Lecture No 7 Object Oriented Programming COMSATS Institute of Information Technology.
Recap Resizing the Vector Push_back function Parameters passing Mechanism Primitive Arrays of Constants Multidimensional Arrays The Standard Library string.
Object Lifetime and Pointers
Dynamic Storage Allocation
CMSC 341 Lecture 2 – Dynamic Memory and Pointers (Review)
Pointers Revisited What is variable address, name, value?
Dynamic Memory CSCE 121 J. Michael Moore.
Chapter 10: Pointers Starting Out with C++ Early Objects Ninth Edition
This pointer, Dynamic memory allocation, Constructors and Destructor
Dynamically Allocated Memory
Dynamic Memory Allocation
Basic C++ What’s a declaration? What’s a definition?
understanding memory usage by a c++ program
Constant pointers and pointers to constants
Indirection.
Destructor CSCE 121 J. Michael Moore.
Destructor CSCE 121.
Review Chapter 10 PPT for full coverage.
Dynamic Memory.
COP 3330 Object-oriented Programming in C++
Introduction to Data Structures and Software Engineering
ENERGY 211 / CME 211 Lecture 10 October 13, 2008.
Dynamic Memory CSCE 121.
Class Circle { Float xc, yc, radius;
Object and its encapsulation
SPL – PS3 C++ Classes.
SPL – PS2 C++ Memory Handling.
Presentation transcript:

CS-1030 Dr. Mark L. Hornick 1 Pointers And Dynamic Memory

CS-1030 Dr. Mark L. Hornick 2 Dynamic Memory C++ has a new () operator like Java new () returns an address of a newly-created object You have to assign the address to a pointer Employee* pe = new Employee;

CS-1030 Dr. Mark L. Hornick 3 Object access via pointers int id; Employee anEmp; // as an object id = anEmp.getID(); // object access Employee* pe = new Employee; // ptr id = pe->getID(); // ptr access //id = (*pe).getID() // never done

CS-1030 Dr. Mark L. Hornick 4 Dynamic Memory C++ does not have a Garbage Collector Who cleans up after you?

CS-1030 Dr. Mark L. Hornick 5 Dynamic Memory C++ does not have a Garbage Collector Who cleans up after you? You do!

CS-1030 Dr. Mark L. Hornick 6 The delete() operator Syntax: delete p; // p is a pointer Rules Make sure p is pointing to a valid object Don’t delete the same object more than once The object must have been created with the new() operator i.e. the object must be on the heap, not the stack Never delete an object created on the stack: Employee e(“Tom”);// e created in stack memory Employee* pe = &e// points at object on stack delete pe;// crash and burn!!!

CS-1030 Dr. Mark L. Hornick 7 delete() and the destructor The destructor is called when you delete an object via the pointer Employee* p = new Employee(“Tom”); delete p; // calls ~Employee() Never dereference the pointer after deleting the object! The pointer still contains the object’s previous address, but the object is no longer valid, even if it’s “hanging around” in memory. Employee* p = new Employee(“Tom”); delete p; // object is invalidated p->giveBigRaise(); // crash and burn !!!!

CS-1030 Dr. Mark L. Hornick 8 Dynamic Memory Operators new Allocates memory from the heap Invokes the constructor (by context) Returns a pointer to the new object delete Invokes the destructor Releases the memory back to the heap

CS-1030 Dr. Mark L. Hornick 9 Object Types static Memory is fixed at compile time Stored in a special area Automatic Dog someDog; Memory allocated as the program runs Stored on the stack (CS280, CS285) Automatically destroyed when it goes out of scope Dynamic Dog* pDog; pDog = new Dog; Drawn from a memory pool (heap) Can be released and the memory reused

CS-1030 Dr. Mark L. Hornick 10 Memory Leaks Common and dangerous problem Sequence of events Dynamic memory is allocated Pointer is lost By reassignment pDog = 0; End of lifetime  pDog goes out of scope Dynamic object is inaccessible Cannot be deleted! Why is this bad?

CS-1030 Dr. Mark L. Hornick 11 NULL Pointer Revisited Purpose This pointer points to no object Value is 0 Defined in Common convention NULL is NEVER dereferenced if new fails, it returns NULL delete “ignores” NULL

CS-1030 Dr. Mark L. Hornick 12 Usage of NULL #include … Dog* pDog = NULL; pDog = new Dog; … delete pDog; pDog = NULL;// For safety