Today’s Topic Dynamic allocation Slides for CS 31 discussion session

Slides:



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

Introduction to Programming Lecture 39. Copy Constructor.
Informática II Prof. Dr. Gustavo Patiño MJ
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Stacks CS-240 & CS-341 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
CS 11 C++ track: lecture 4 Today: More on memory management the stack and the heap inline functions structs vs. classes.
1 Data Structures - CSCI 102 CS102 C++ Pointers & Dynamic Objects Prof Tejada.
1 Writing a Good Program 8. Elementary Data Structure.
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.
Object-Oriented Programming in C++
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
 Memory from the heap  Dynamic memory allocation using the new operator  Build a dynamic linked list  Another way of traversing a linked list 
CS162 - Topic #6 Lecture: Pointers and Dynamic Memory –Review –Dynamically allocating structures –Combining the notion of classes and pointers –Destructors.
1 Linked Multiple Queues. 2 A real world example. Not in the book. Sometimes we have a fixed number of items that move around among a fixed set of queues.
Dynamic Memory Management & Static Class Members Lecture No 7 Object Oriented Programming COMSATS Institute of Information Technology.
POINTERS AND MEMORY ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED BY NANCY M. AMATO AND JORY DENNY 1.
Pointers and Dynamic Arrays
CMSC 341 Lecture 2 – Dynamic Memory and Pointers (Review)
Pointers and Memory Overview
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
Pointers Psst… over there.
LinkedList Class.
Programmazione I a.a. 2017/2018.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Dynamically Allocated Memory
Pointers Psst… over there.
Chapter 16-2 Linked Structures
Chapter 15 Pointers, Dynamic Data, and Reference Types
Pointers, Dynamic Data, and Reference Types
Dynamic Memory A whole heap of fun….
Dynamic Memory Allocation
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.
Pointers & Dynamic Memory
Indirection.
Dynamic Memory A whole heap of fun….
Dynamic Memory A whole heap of fun….
Today’s Topic Const Ref:
Destructor CSCE 121 J. Michael Moore.
Class and Objects In a class, all the functions that operate on the data structure are grouped together in one place along with the data Like a struct.
Destructor CSCE 121.
Passing Arguments and The Big 5
Stacks CS-240 Dick Steflik.
Dynamic Memory A whole heap of fun….
Dynamic Memory.
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
The Stack.
COP 3330 Object-oriented Programming in C++
CS31 Discussion 1H Winter19: week 9
Class: Special Topics 2 For classes using memory allocation
CS31 Discussion 1D Winter19: week 4
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Pointers and References
CS31 Discussion 1H Fall18: week 9
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Dynamic Memory CSCE 121.
Destructors, Copy Constructors & Copy Assignment Operators
Pointers, Dynamic Data, and Reference Types
Destructors, Copy Constructors & Copy Assignment Operators
CS 144 Advanced C++ Programming April 30 Class Meeting
Object and its encapsulation
Lesson 13 Pointers & Dynamic Memory
Presentation transcript:

Today’s Topic Dynamic allocation Slides for CS 31 discussion session TA: Hsiao-Yun (Katie) Tseng tsenghy@g.ucla.edu Credit to former TA Bo-Jhang Ho (bojhang@cs.ucla.edu), CS31 Discussion 1E, Spring 17’ Credit to former TA Chelsea Ju (chelsea.ju@cs.ucla.edu)

What is “new” Dynamically allocate memory Step 1: allocate memory Step 2: return the memory address Don’t forget to return the memory delete operator Don’t lose the pointer, otherwise you will never be able to recycle the allocated memory!

Stack / heap

Stack / heap Local variables go here! Dynamic memory allocations go here!

Local variable v.s. dynamic allocation Local variables are easier to manage The last showing up variable is going recycled first Dynamic memory allocation A more flexible way to get available memory! There is no way to determine when an allocated memory piece is going to be freed. Developers need to explicitly say which memory have to be recycled. Memory fragmentation problem

Constructor / destructor Constructor is for class (structure) initialization Destructor gives the victim a chance to say good bye to the main program before it gets recycled Usually we don’t need to override the default destructor, but if you dynamically allocate memory in constructor, this is the only chance you can free the allocated memory.

Constructor / destructor class Pit { public: Scorpion *sa; Scorpion *sb; int thirdVal; Pit() { sa = new Scorpion; sb = new Scorpion; thirdVal = 10; } ~Pit() { delete sa; delete sb; } };

Constructor / destructor class Pit { public: Scorpion *sa; Scorpion *sb; int thirdVal; Pit(); ~Pit(); }; Pit::Pit() { sa = new Scorpion; sb = new Scorpion; thirdVal = 10; } ~Pit::Pit() { delete sa; delete sb; }

An array of pointers Human* object[10]; nullptr nullptr nullptr

An array of pointers struct Human { string m_name; Human(string name) : m_name(name) cout << m_name << " says hi" << endl; } ~Human() cout << m_name << " says bye" << endl; }; class Map { private: Human* player[10]; int n_player = 0; int counter = 0; public: void add_player(int n); void remove_player_at(int index);

An array of pointers n_player=8 void Map::add_player(int n) { for (int i=0; i<n; i++) if (n_player < 10) player[n_player] = new Human("player" + to_string(counter++)); n_player++; } else break; n_player=8 Player7 is the newest player on the map

An array of pointers Add new player n_player=8 void Map::add_player(int n) { for (int i=0; i<n; i++) if (n_player < 10) player[n_player] = new Human("player" + to_string(counter++)); n_player++; } else break; Add new player n_player=8 Player7 is the newest player on the map

An array of pointers Remove player at index 3 n_player=8 void Map::remove_player_at(int index) { delete player[index]; for (int i=index; i<n_player-1; i++) player[i] = player[i+1]; } player[n_player-1] = NULL; n_player--; Remove player at index 3 n_player=8

An array of pointers NULL void Map::remove_player_at(int index) { delete player[index]; for (int i=index; i<n_player-1; i++) player[i] = player[i+1]; } player[n_player-1] = NULL; n_player--; NULL

An array of pointers We can deallocate the memory in destructor. class Map { private: Human* player[10]; int n_player = 0; int counter = 0; public: void add_player(int n); void remove_player_at(int index); ~Map() { for (int i=0; i<n_player; i++) delete player[i]; } }; int main() Map map; map.add_player(2); map.remove_player_at(0); map.add_player(5); map.remove_player_at(3); return 0; We can deallocate the memory in destructor. Make sure to deallocate the memory you allocated! Otherwise the program will crash  (at least in Linux it will)