introduction to memory management n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Introduction to Memory Management PowerPoint Presentation
Download Presentation
Introduction to Memory Management

Loading in 2 Seconds...

play fullscreen
1 / 26

Introduction to Memory Management - PowerPoint PPT Presentation


  • 86 Views
  • Uploaded on

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

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Introduction to Memory Management' - damali


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
introduction to memory management
Introduction to

Memory Management

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
    • Implementation of a language standard.
  • Depends on a platform
    • E.g., size of a stack.
static storage c
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
Stack or Automatic Storage (C++)
  • All local variables
  • Example

void main () { // or any other function

int a3;

MyClass c3;

}

heap or dynamic memory c
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
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 …
slide9

Dynamic Memory Allocation for the One-Dimensional Array

void main() {

int *A = new int[10];

delete [] A;

}

slide10

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;

}

slide11

Sample Structure Representing an Individual Employee

typedef struct {

int id;

char name[15];

} EmployeeT;

void main() {

EmployeeT *employee = new EmployeeT;

delete employee;

}

garbage collection
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.
slide13

Problems with Pointers

  • Dangling pointers (widows)
  • Lost heap variables or memory leakage (orphans)
    • Garbage collection should be able to handle this problem
garbage collection1
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 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 counting1
Reference counting
  • Problem: can not detect inaccessible circular chain
mark sweep
Mark-Sweep
  • Every heap cell has an extra bit used by collection algorithm
  • All cells initially set to garbage
mark sweep1
Mark-Sweep

All pointers traced into heap, and reachable cells marked as not garbage

mark sweep2
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
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 overflows1
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 overflows2
Special Topic: Buffer Overflows
  • Must read: http://en.wikipedia.org/wiki/Buffer_overflow
slide23
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?

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

slide25
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;

}

slide26
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;

}