1 / 27

Memory Management

Memory Management. CS101 2012.1. Variable storage thus far. Never used global variables All variables allocated inside functions, passed by value or reference to other functions In case of non-collection variables, the storage was always taken from the stack

pilis
Download Presentation

Memory Management

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Memory Management CS101 2012.1

  2. Variable storage thus far • Never used global variables • All variables allocated inside functions, passed by value or reference to other functions • In case of non-collection variables, the storage was always taken from the stack • In case of collection variables, storage was taken from the stack and the heap, but we did not need to understand how, or interfere Chakrabarti

  3. Want a variable to outlive its scope • Same customer can have a checking account and a fixed deposit • Should not make copies of customer record • Supposing address or phone changes Customer makeCustomer(…) { … } Vs. Customer *makeCustomer(…) { … } • For simplicity let’s start with int variables Chakrabarti

  4. new and delete pi points to an integer int *pi = new int; *pi = 5; cout << pi << ' ' << *pi + 1 << endl; delete pi; Allocates four bytes fom heap The value is always accessed as *pi The value is always accessed as *pi Returns four allocated bytes to system: do this exactly once! Chakrabarti

  5. Pointer syntax TypeName *pointerVariableName; • TypeName is int, float, double etc. or the name of a class like vector<int> etc. • * is the “pointer access” or “pointer dereference” operator • To read or write the storage holding the value of type TypeName, use*pointerVariableNamein either the lhs or rhs • To read or write the pointer itself use pointerVariableName without a star Chakrabarti

  6. Allocating arrays in the heap int nn = 5; int *pi = new int[nn]; for (int ix=0; ix < nn; ++ix) { cout << pi[ix] << endl; cout << *(pi+ix) << endl; } delete [] pi; Allocates in heap space for nn ints Type remains same Looks like array access Contents of cell ix integers after address pi Returns space back to heap (which remembers size but won’t tell you) Chakrabarti

  7. The null pointer • Address 0 is not a legal address to read/write • Programs reliably fail if they attempt to do so • Therefore, int *pa = 0; is a safe initializer until memory is allocated (or after it is freed) • But 0 is of type int, while pa is of type int * (pointer to int) • Therefore a type cast is requiredint *pa = (int*) 0; • Can do this will any type in place of int Chakrabarti

  8. Null pointer for classes class Customer { public: static Customer * const null; }; Customer * const Customer::null = (Customer*) 0; • Can now use Customer::null anywhere • Cleaner than (Customer*) 0 everywhere Chakrabarti

  9. Our own vector class: specification class Vector { public: Vector(); Vector(const Vector& other); ~Vector(); void insert(int p, float v); float remove(int p); float get(int p) const; int size() const; } Default constructor: empty vector Copy constructor Destructor Promises that these methods will not modify the Vector in any way Chakrabarti

  10. Vector implementation • Three private fields • Native array pa of floats allocated on heap • int cap recording the number of floats that can fit in the array (capacity) • int siz recording the number of floats currently in the array (from position 0 onward) • Invariant: siz cap • Run out of space: allocate larger array, copy • siz<< cap: allocate smaller array, copy Chakrabarti

  11. insert void insert(int p, float v) { if (siz + 1 <= cap) { // enough space } else { float *nPa = new float[siz+1]; int wx = 0, rx = 0; while (rx < p) { nPa[wx++] = pa[rx++]; } nPa[wx++] = v; while (rx < siz) { nPa[wx++] = pa[rx++]; } if (cap > 0) { delete [] pa; } cap = siz = siz + 1; pa = nPa; } } p pa nPa v Chakrabarti

  12. Our own queue class: specification class Queue { // of ints, say public: bool isEmpty() const; int removeFirst(); void pushLast(int val); void print() const; private: QueueElement *first, *last; }; Chakrabarti

  13. Queue implementation struct QueueElement { int value; QueueElement *next; QueueElement(int v) : value(v), next(null) { } } Chakrabarti

  14. Printing a queue QueueElement *pqe = first; while (pqe != null) { cout << pqe->value << endl; pqe = pqe->next; } value next value next pqe Chakrabarti

  15. Constructor and destructor Queue::Queue() { first=last=null; } Queue::~Queue() { while (first != null) { QueueElement *pQe = first; first = first->next; delete pQe; } } first next pQe Chakrabarti

  16. pushLast void Queue::pushLast(int val) { QueueElement *pQe = new QueueElement(val); if (first == null) { first = pQe; } if (last != null){ last->next=pQe; } last = pQe; } val pQe last Chakrabarti

  17. removeFirst int Queue::removeFirst() { int ans = first->value; QueueElement *pQe = first; first = first->next; delete pQe; return ans; } Chakrabarti

  18. Binary search tree • A binary tree is either empty • Or it has a root node • With two children left and right • Each of which is a binary tree • Suppose each node contains an int key • Assume no duplicate keys for starters • The tree is a search tree if • All keys in left are smaller than the root • All keys in right are larger than the root Chakrabarti

  19. Search tree from main() TreeNode *root = null; for (;;) { int key; cin >> key; if (root == TreeNode::null) { root = new TreeNode(key); } else { root->insert(key); } } Chakrabarti

  20. Definition of the tree node struct TreeNode { const int key; TreeNode *left, *right; TreeNode(int _key) : key(_key) { left = right = null; } void insert(int nKey) { // while satisfying property } }; Chakrabarti

  21. insert, first cut if (nKey < key) { if (left == null) { left = new TreeNode(nKey); } else { left->insert(nKey); } } else { // repeat above, right instead of left } Tedious, can be avoided if you have a good understanding of * and & Chakrabarti

  22. void insert(int nKey) TreeNode*& child = nKey < key? left : right; if (child == null) { child = new TreeNode(nKey); } else { child->insert(nKey); } child is a reference to a pointer to a TreeNode Recursive call from child node Chakrabarti

  23. How to print the keys in order void print() { if (left != null) left->print(); cout << key << ' '; if (right != null) right->print(); } main() { if (root != null) root->print(); cout << endl; } 2 1 3 Chakrabarti

  24. Adding a parent pointer • Walking down from parent to child is easy • Sometimes handy to be able to walk up from child to parent as well, e.g., to find siblings • root->up is null struct TreeNode { const int key; TreeNode *left, *right, *up; TreeNode(int _key, TreeNode *_up) : key(_key), up(_up) { left = right = null; } void insert(int nKey) { … } }; Chakrabarti

  25. Constant pointer vs. pointer to constant • Suppose we want the up pointer to be constant • This is declared asTreeNode * const up; • Content of TreeNode can change • A pointer to a constant TreeNode would beconst TreeNode * up; • Not the same! • Can also haveconst TreeNode * const up; Chakrabarti

  26. Pointers vs. references int a = 3, b = 5; int *px = &a; px = &b; // above statements do not // change values in cells // called a and b int &rx = a; rx = b; // this results in a = b = 5 Chakrabarti

  27. Multiple pointers to one record • Generally necessary in applications • E.g., fixed deposit accounts and checking accounts pointing to shared customer record • Care is needed in destructors • Deleting a checking account should not destroy a customer record • Should deleting the last account of a customer delete the customer record? • Clear “home” collection whose destruction deletes customer record Chakrabarti

More Related