Download - CSC 213 – Large Scale Programming. Explicit Memory Management Traditional form of memory management Used a lot, but fallen out of favor malloc

Transcript

CSC 213 Large Scale Programming Explicit Memory Management Traditional form of memory management Used a lot, but fallen out of favor malloc / new Commands used to allocate space for an object free / delete Return memory to system using these command Simple to use Explicit Memory Management Traditional form of memory management Used a lot, but fallen out of favor malloc / new Commands used to allocate space for an object free / delete Return memory to system using these command Simple to use, but tricky to get right memory leak Forget to free memory leak dangling pointer free too soon dangling pointer Dangling Pointers Node x = new Node(happy); Dangling Pointers Node x = new Node(happy); Node ptr = x; Node x = new Node(happy); Node ptr = x; Dangling Pointers Node x = new Node(happy); Node ptr = x; delete x; // But Im not dead yet! Node x = new Node(happy); Node ptr = x; delete x; // But Im not dead yet! Dangling Pointers Node x = new Node(happy); Node ptr = x; delete x; // But Im not dead yet! Node y = new Node(sad); Node x = new Node(happy); Node ptr = x; delete x; // But Im not dead yet! Node y = new Node(sad); Dangling Pointers Node x = new Node(happy); Node ptr = x; delete x; // But Im not dead yet! Node y = new Node(sad); cout data