Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Today’s Topic Dynamic allocation Slides for CS 31 discussion session"— Presentation transcript:

1 Today’s Topic Dynamic allocation Slides for CS 31 discussion session
TA: Hsiao-Yun (Katie) Tseng Credit to former TA Bo-Jhang Ho CS31 Discussion 1E, Spring 17’ Credit to former TA Chelsea Ju

2 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!

3 Stack / heap

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

5 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

6 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.

7 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; } };

8 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; }

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

10 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);

11 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

12 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

13 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

14 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

15 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)


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

Similar presentations


Ads by Google