270 likes | 386 Views
Introduction to Memory Management. General Structure of Run-Time Memory. Structure of Run-Time Memory. Depends on a programming language E.g., in C++, a class object can be allocated in static storage, stack or heap. In Java, only heap. Depends on a compiler
E N D
Introduction to Memory Management
Structure of Run-Time Memory • Depends on a programming language • E.g., in C++, a class object can be allocated in static storage, stack or heap. In Java, only heap. • Depends on a compiler • Implementation of a language standard. • Depends on a platform • E.g., size of a stack.
Static storage (C++) • Global variables • Static local variables • Example int a1; MyClass c1; void main () { // or any other function static int a2; static MyClass c2; }
Stack or Automatic Storage (C++) • All local variables • Example void main () { // or any other function int a3; MyClass c3; }
Heap or Dynamic Memory (C++) • Variables (objects) createdwith • 'new', 'new[]' , 'calloc()', 'malloc()' or'realloc()' • Example int *p1 = new int; MyClass *p2 = new MyClass(); void main () { // or any other function int *p3 = new int[50]; MyClass *p4 = new MyClass[50]; … delete [] p3; delete [] p4; } delete p1; delete p2;
Stack and Subprograms • Local (non-static) variables are stored in the stack as part of so called activation record instance (or stack frame). • Each activation record instance corresponds to a subprogram call. • We will study subprograms in more detail in Chapters 9 & 10 …
Dynamic Memory Allocation for the One-Dimensional Array void main() { int *A = new int[10]; … delete [] A; }
Memory Allocation for Two-Dimensional Array // code does not exactly correspond to the figure void main() { int **C; C = new int*[4]; for(int i = 0; i < 4; i++) { C[i] = new int[3]; } … for(i = 0; i < 4; i++){ delete [] C[i]; } delete [] C; }
Sample Structure Representing an Individual Employee typedef struct { int id; char name[15]; … } EmployeeT; void main() { EmployeeT *employee = new EmployeeT; … delete employee; }
Garbage Collection • Heap may contain objects (memory chunks) that are no longer needed. These objects are known as garbage, since they have no useful purpose once they are discarded. • In C++, a programmer must take care of garbage. Note that I used ‘delete’ or ‘delete []’. • In Java, automatic garbage collection is used by the language system. A programmer does not need (can not) to free memory explicitly.
Problems with Pointers • Dangling pointers (widows) • Lost heap variables or memory leakage (orphans) • Garbage collection should be able to handle this problem
Garbage Collection • We consider two strategies/algorithms: • Reference counting (eager approach) • Mark-sweep (lazy approach) • Initial (empty) heap is represented as a continuous chain of nodes (cells) called the free_list • Real algorithms can be much more complex
Reference counting • Reference counters: maintain a counter in every cell that store the number of pointers currently pointing at the cell • Disadvantages: space required, complications for cells connected circularly
Reference counting • Problem: can not detect inaccessible circular chain
Mark-Sweep • Every heap cell has an extra bit used by collection algorithm • All cells initially set to garbage
Mark-Sweep All pointers traced into heap, and reachable cells marked as not garbage
Mark-Sweep All garbage cells returned to list of available cells (free_list) Disadvantages of mark-sweep algorithm: when you need it most, it works worst (takes most time when program needs most of cells in heap)
Special Topic: Buffer Overflows • A buffer overflow is an exploit that takes advantage of a program that is waiting on a user's input • A favorite exploit for hackers (most frequently used) • The vast majority of Microsoft's available patches fix unchecked buffer problems • Other applications may have the same problems
Special Topic: Buffer Overflows • There are two main types of buffer overflow attacks: • Stack-based (most common) • Heap-based. (difficult and therefore rare) • It is therefore critical that you understand how they work and perform vulnerability testing on your home-grown applications prior to deployment
Special Topic: Buffer Overflows • Must read: http://en.wikipedia.org/wiki/Buffer_overflow
Quiz • Question 1 • Consider the following C++ statement that appears outside of a function. int *A = new int[10]; Where and how much memory will be allocated when the statement executes?
Quiz • Question 2 • Consider the following C++ statement that appears inside of a function. int *A = new int[10]; Where and how much memory will be allocated when the statement executes?
Quiz • Question 3 • Is there a problem with the following C++ program? int * init(){ int a[50]; // initialize array return a; } void main(){ int * a = init(); // print array; }
Quiz • Question 4 • Is there a problem with the following C++ program? int * init(){ int *a = new int[50]; // initialize array return a; } void main(){ int * a = init(); // print array; }