SLIDE 1 CS31 Discussion 1E
Spring 17’: week 10
TA: Bo-Jhang Ho
bojhang@cs.ucla.edu
Credit to former TA Chelsea Ju
SLIDE 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!
SLIDE 3
Stack / heap
SLIDE 4
Stack / heap
Local variables go here! Dynamic memory allocations go here!
SLIDE 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
SLIDE 6
An array of pointers
} Scorpion* objects[10];
SLIDE 7
Accessing member operator
} Dot (.) operator
} Scorpion scor;
scor.m_row = 3;
} Arrow (->) operator
} Scorpion *ptr = new Scorpion;
ptr->m_row = 3;
} Just treat dot (.) and arrow (->) operator as ‘s
SLIDE 8
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.
SLIDE 9 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; } };
SLIDE 10 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; }
SLIDE 11
Toward object-oriented
} Treat things as objects, capture interaction of objects } Consider variables in a class as states
} Usually variables are declared as private } Which means beyond the scope of the class, nobody can
arbitrarily change the state
} Consider methods as ways to change states
} To enforce users to change states in a certain ways } Maintain the states, keep them valid
} A method is a function in a class
SLIDE 12
What is this
} this is a built-in keyword } this can only be used in a method } this means the owner of the method } this is a pointer } Why do we need to have the keyword this?
} Distinguish local variable from instance variable } Pass myself to others
SLIDE 13 What is this
class Test { public: int score; Test(); saveScore(); }; Test::Test() { score = 60; } Test::saveScore(int score) { score = score; }
SLIDE 14 What is this
class Test { public: int score; Test(); saveScore(); }; Test::Test() { score = 60; } Test::saveScore(int score) { this->score = score; }
SLIDE 15 What is this
Player::Player(Pit* pp, int r, int c) { ... m_pit = pp; ... } bool Pit::addPlayer(int r, int c) { ... // Dynamically allocate a new Player and add it to the pit m_player = new Player(this, r, c); return true; }