Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Memory Management. 2 General Structure of Run-Time Memory.

Similar presentations


Presentation on theme: "Introduction to Memory Management. 2 General Structure of Run-Time Memory."— Presentation transcript:

1 Introduction to Memory Management

2 2 General Structure of Run-Time Memory

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

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

5 5 Stack or Automatic Storage (C++) All local variables Example void main () { // or any other function int a3; MyClass c3; }

6 6 Heap or Dynamic Memory (C++) Variables (objects) created with –'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;

7 7 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 …

8 8 A closer look at the heap.

9 9 Dynamic Memory Allocation for the One-Dimensional Array void main() { int *A = new int[10]; … delete [] A; }

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

11 11 Sample Structure Representing an Individual Employee typedef struct { int id; char name[15]; … } EmployeeT; void main() { EmployeeT *employee = new EmployeeT; … delete employee; }

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

13 13 Problems with Pointers Dangling pointers (widows) Lost heap variables or memory leakage (orphans) –Garbage collection should be able to handle this problem

14 14 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

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

16 16 Reference counting Problem: can not detect inaccessible circular chain

17 17 Mark-Sweep Every heap cell has an extra bit used by collection algorithm All cells initially set to garbage

18 18 Mark-Sweep All pointers traced into heap, and reachable cells marked as not garbage

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

20 20 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

21 21 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

22 22 Special Topic: Buffer Overflows Must read:

23 23 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?

24 24 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?

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

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


Download ppt "Introduction to Memory Management. 2 General Structure of Run-Time Memory."

Similar presentations


Ads by Google