Dynamic Memory Allocation (also see pointers lectures) -L. Grewe.

Slides:



Advertisements
Similar presentations
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
Advertisements

Dynamic memory allocation
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.
Carnegie Mellon 1 Dynamic Memory Allocation: Basic Concepts : Introduction to Computer Systems 17 th Lecture, Oct. 21, 2010 Instructors: Randy Bryant.
Chapter 6 Data Types
Derived data types Dealing with data –Where the information is stored –What value is kept there –What kind of information is stored Address operator Pointers.
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
A pointer is the memory address of a variable. A memory address is a physical location within a system’s memory space. A pointer variable is variable used.
Introduction to Programming Lecture 39. Copy Constructor.
Introduction of Programming Lecture 28. Today’s Lecture How memory allocation is done in How memory allocation is done in C++ C++ How is it different.
Informática II Prof. Dr. Gustavo Patiño MJ
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.
Dynamic Objects. COMP104 Lecture 31 / Slide 2 Static verses Dynamic Objects * Static object n Memory is acquired automatically  int A[10]; n Memory is.
Introduction to Data Structure, Spring 2007 Slide- 1 California State University, Fresno Introduction to Data Structure Memory Allocation Ming Li Department.
Lecture 2 Pointers Pointers with Arrays Dynamic Memory Allocation.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
Addresses in Memory When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location. This is.
Pointers Chapter 9. Getting The Address Of A Variable Each variable in program is stored at a unique address Use address operator & to get address of.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Dynamic Memory Allocation 9.8.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
7. Pointers, Dynamic Memory 20 th September IIT Kanpur 1C Course, Programming club, Fall 2008.
This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson.
Stack and Heap Memory Stack resident variables include:
Storage Bindings Allocation is the process by which the memory cell or collection of memory cells is assigned to a variable. These cells are taken from.
Pointers review Let a variable aa be defined as ‘int *aa;’, what is stored in aa? Let a variable aa be defined as ‘int ** aa;’ what is stored in aa? Why.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
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.
Dynamic memory allocation and Pointers Lecture 4.
Current Assignments Start Reading Chapter 6 Project 3 – Due Thursday, July 24 Contact List Program Homework 6 – Due Sunday, July 20 First part easy true/false.
Dynamic Memory. We will follow different order from Course Book We will follow different order from Course Book First we will cover Sect The new.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
UFS003C3 Lecture 15 Data type in C & C++ Using the STL.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved This Weeks Topics: Pointers (continued)  Modify C-String through a function call 
Revision on C++ Pointers TCP1201: 2013/2014. Pointer Basics  Why pointer is important? 1. Reference/Point to existing data without cloning the data.
POINTERS IN C. Introduction  A pointer is a variable that holds a memory address  This address is the location of another object (typically another.
P OINTERS A pointer is an address All data is stored in memory in some location that is indexed with an address Can refer to variables by name or by memory.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 24: Pointers and Dynamic Allocation.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 7 – Pointers.
By Anand George SourceLens.org Copyright. All rights reserved. Content Owner - Meera R (meera at sourcelens.org)
 Memory from the heap  Dynamic memory allocation using the new operator  Build a dynamic linked list  Another way of traversing a linked list 
Container Classes. How do we do better? Tough to delete correctly: startLocation.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Linked Lists Outline Introduction Self-Referential Structures.
POINTERS AND MEMORY ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED BY NANCY M. AMATO AND JORY DENNY 1.
Introduction to Programming
C++ Interlude 2 Pointers, Polymorphism, and Memory Allocation
Dynamic Memory CSCE 121 J. Michael Moore.
Dynamic Memory Allocation
Lecture 11 Memory Richard Gesick.
Dynamic Memory A whole heap of fun….
Dynamic Memory Copy Challenge
Dynamic Memory Allocation
Pointers And Memory Acknowledgement: THE Slides are Prepared FROM SLIDES PROVIDED By NANCY M. AMATO AND Jory Denny.
By Hector M Lugo-Cordero September 17, 2008
Review & Lab assignments
Dynamic Memory A whole heap of fun….
Dynamic Memory A whole heap of fun….
Chapter 17: Linked Lists.
Jeff West - Quiz Section 8
7. Pointers, Dynamic Memory
Dynamic Memory A whole heap of fun….
C Programming Lecture-8 Pointers and Memory Management
C Programming Pointers
Dynamic Memory And Objects
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Dynamic Memory Copy Challenge
Dynamic Memory CSCE 121.
Pointers, Dynamic Data, and Reference Types
Run-time environments
Presentation transcript:

Dynamic Memory Allocation (also see pointers lectures) -L. Grewe

Objectives What is Dynamic Memory Allocation How to do memory allocation featuring new.

Dynamic Memory Allocation At run time to allocate memory using pointers to point to it. Different ways: new malloc(), alloc( ), free ( ) 3

Dynamic Allocation using new The object is stored in a large free memory area called the heap (or free-store). When created in this way, the object remains on the heap until you remove it. The delete operator erases the object from the heap.

Creating an Object – allocation via new int * P = new int; Using the new operator, we create an int object on the heap and assign its address to P. *P = 25; // assign a value cout << *P << endl; Now we can use the pointer in the same way as previous examples.

new and delete Student * pS = new Student;. // use the student for a while.... delete pS; // gone! The new operator returns the address of a new object. The delete operator erases the object and makes it unavailable. Student constructor called

The new Operator allocates memory and return a pointer?p1900 int *p1; p1 = new int; *p1 = 20;?? p1 points to a dynamic integer variable without any identifier (name) - p1 points to a dynamic integer variable without any identifier (name) - dynamic memory comes from the programs’ heap (free store) 20?10500 ? … … p1?

Dynamic Arrays new can allocate an entire array all at once?p1900 int *p1; p1 = new int[4]; p1[2] = 20; cout<<*(p1+2); p1 points to 1st entry of dynamic array - p1 points to 1st entry of dynamic array - number of entries in a pair of sq. brackets - two ways to access p1 (array or pointer) ? … … p1?

Accessing Dynamic Array Use array notation – the 1 st entry p1[0] = 18; – the 3 rd entry p1[2] = 20; – the ith entry p1[i-1] = 19; Use pointer notation – the 1 st entry *p1 = 18; – the 3 rd entry *(p1+2) = 20; – the ith entry *(p1+i-1) = 19;

Dynamic Array Example A program read ages of each student in a CS class, with varying sizes, calculate the average, and then print out the average. size_t size; int *ages; float average; cin >> size; ages = new int[size]; // input ages of all students // calculate average // print average …

Dynamic Objects of a class new can also allocate a dynamic object?p1900 point *p1; p1 = new point(1.0, 2.0); cout<< (*p1).get_x(); cout get_x(); - p1 points to dynamic object without name - p1 points to dynamic object without name - parameters can be used as in declaration - two ways to access p1 (* and ->) ? … … p1?

Failure of the new Operator Dynamic memory via new operator comes from heap of a program Heap size from several K to GB, however fixed Could run out of room therefore cause a bad_alloc exception – error message and program halts Good practice 1: document which functions uses new Good practice 2: garbage collection by delete operator

Releasing Memory Some languages like Java, C# have garbage collectors. In C/C++ you can deallocate memory directly delete dealloc, free 13

The delete Operator Release any dynamic memory (heap memory) that is no longer needed int *i_ptr; double *d_ptr; point *p_ptr; i_ptr = new int; d_ptr = new double[20]; p_ptr = new point(1.0, 2.0); … delete i_ptr; delete [ ] d_ptr; // empty brackets delete p_ptr; Questions( true or false): 1.delete resets these pointers 2.delete removes dynamic objects pointed by the pointers 3.nothing happens to the pointers themselves FTT

Using new in Functions void MySub() { Student * pS = new Student; // use the Student for a while... delete pS; // delete the Student } // pS disappears If you create an object inside a function, you may have to delete the object inside the same function. In this example, variable pS goes out of scope at the end of the function block.

Memory Leak with Objects void MySub() { Student * pS = new Student; // use the Student for a while... } // pS goes out of scope (the Student's still left on the heap) is an error condition that is created when an object is left on the heap with no pointer variable containing its address. This might happen if the object's pointer goes out of scope: