Docsity

Spring 2003 CS 403: Lecture Notes on Pointers, Memory Leaks, and Garbage Collection, Study notes of Programming Languages

Toggle sidebar
bg1
1
Spring 2003 CS 403 Class Notes Page 1
Lecture 9 - Thursday, 2/6/03
• Today
– A little more on Java and pointers
– Memory Leaks
• Reading Assignments For Next Class
– pp. 189-191, 388-94
• Assignments
– Assignment #2 due tonight
• Exam 1
– Tuesday, February 11
– Open book, open notes
Spring 2003 CS 403 Class Notes Page 2
Pointer
• A reference to an object
– Normally implemented as an address
• Some languages restrict reference to be only
heap objects (Ada 83, Pascal)
• Other languages allow pointers to non-heap
objects (C++):
int *x;
int y;
x = &y;
Spring 2003 CS 403 Class Notes Page 3
Pointer Design Issues
• Dereferencing alternatives:
– Implicit
– Explicit
• Deallocation alternatives:
– Explicit deallocation operator in the language
– Automatic storage reclamation
• Garba ge collection
– Some combination of both garbage collection and
explicit deallocation.
Spring 2003 CS 403 Class Notes Page 4
Fundamental Pointer Operations
• Memory allocation:
int *x; x = new int;
int *x; x = new int[50];
Stack *s; s = new Stack;
• Dereferences:
– Explicit
– Implicit
Spring 2003 CS 403 Class Notes Page 5
Implicit vs. Explicit Dereferencing
• Explicit (C++):
int *x, *y;
x = new int;
*x = 50;
*y = 48;
*y = *x;
• Implicit: A reference to a pointer variable is an
automatic dereference:
– y = x would function as *y = *x;
Spring 2003 CS 403 Class Notes Page 6
A More Realistic Example
C++ (Explicit Deference):
class Node{
public:
int x;
Node *next;
};
Node *n;
n = new Node;
(*n).x = 3;
n->x = 3;
Ada (Implicit deref for recs):
type Node is record
x: integer;
next: access Node;
end Node;
n: access Node;
n = new Node;
n.x = 3
pf3
pf4
pf5

Partial preview of the text

Download Spring 2003 CS 403: Lecture Notes on Pointers, Memory Leaks, and Garbage Collection and more Study notes Programming Languages in PDF only on Docsity!

Spring 2003 CS 403 Class Notes (^) Page 1

Lecture 9 - Thursday, 2/6/

  • Today
    • A little more on Java and pointers
    • Memory Leaks
  • Reading Assignments For Next Class
    • pp. 189-191, 388-
  • Assignments
    • Assignment #2 due tonight
  • Exam 1
    • Tuesday, February 11
    • Open book, open notes

Spring 2003 CS 403 Class Notes (^) Page 2

Pointer

  • A reference to an object
    • Normally implemented as an address
  • Some languages restrict reference to be only

heap objects (Ada 83, Pascal)

  • Other languages allow pointers to non-heap

objects (C++):

int *x;

int y;

x = &y;

Pointer Design Issues

  • Dereferencing alternatives:
    • Implicit
    • Explicit
  • Deallocation alternatives:
    • Explicit deallocation operator in the language
    • Automatic storage reclamation
      • Garbage collection
    • Some combination of both garbage collection and

explicit deallocation.

Spring 2003 CS 403 Class Notes (^) Page 4

Fundamental Pointer Operations

  • Memory allocation: int *x; x = new int; int *x; x = new int[50]; Stack *s; s = new Stack;
  • Dereferences:
    • Explicit
    • Implicit

Spring 2003 CS 403 Class Notes (^) Page 5

Implicit vs. Explicit Dereferencing

  • Explicit (C++): int *x, *y; x = new int; *x = 50; *y = 48; *y = *x;
  • Implicit: A reference to a pointer variable is an

automatic dereference:

  • y = x would function as *y = *x;

A More Realistic Example

C++ (Explicit Deference):

class Node{ public: int x; Node *next; };

Node n; n = new Node; (n).x = 3; n->x = 3;

Ada (Implicit deref for recs):

type Node is record x: integer; next: access Node; end Node;

n: access Node; n = new Node; n.x = 3

Spring 2003 CS 403 Class Notes (^) Page 7

References in C++

• Normally used for parameters

• Hold addresses

• Two rules:

  • Initialized to an address
  • Implicitly dereferenced in every non-initialization

use

void foo(int &x){ int y = 0; x = y; y = x; }

Spring 2003 CS 403 Class Notes (^) Page 8

Pointer Arithmetic

• The following is valid in C/C++

int n; int *a; int b[10]; a = b; n = a[3]; n = *(a + 3); n = b[3]; n = *(b + 3);

Arrays: Stack vs. Heap Allocation

• C++:

  • int a[50] --Allocates the array on the stack
  • int *a or a[] --Allocates a pointer on the stack

• The second declaration must be followed with

a new:

  • a = new int[x];

Spring 2003 CS 403 Class Notes (^) Page 10

Java and Objects

• All objects in Java are allocated on the heap

• Declaration:

  • Stack s;
  • Implementation: Record (array, integer)
    • Java: Puts a pointer in the activation record
    • C++: Puts the whole stack in the activation record
  • Must allocate space for the object in Java
    • S = new Stack;
  • Dereferencing in Java is then implicit (s.push()

rather than s -> push())

Spring 2003 CS 403 Class Notes (^) Page 11

Problems with Pointers

1. Dangling reference (already covered)

  • Pointer refers to an object that is no longer valid (space has been reclaimed)
  • Dangerous!

2. Memory leaks

  • Space on the heap has no pointers pointing to it
  • Wastes space
  • A long running program might run out of space

Memory Leaks

• Simplest version:

void main(void){ int *x, *y; x = new int; y = new int; *y = 3; x = y; }

Spring 2003 CS 403 Class Notes (^) Page 19

What Garbage Collection Will Do

void foo(void){ Node *head; //manufacture linked list pointed to by //head }

In this example, garbage collection will walk through the entire list and free up every node (once head is dead) No explicit list traversal to free up memory is needed Java supports this approach and provides automatic garbage collection – it does not provide a “delete” operator.

Spring 2003 CS 403 Class Notes (^) Page 20

Approaches to Garbage Collection

  • Reference counts: Don’t always work well.
  • Mark and sweep: Works better than reference

counts, but much more complicated.

  • Typical approaches are periodic: At some frequency,

the garbage collection routine runs (resulting in

periodic performance costs)

Reference Counts

  • For every heap object:
    • Keep a counter of the number of pointers pointing to that object
    • Modify the reference counts every time a pointer changes (either up or down as appropriate)
    • When a reference count reaches 0, reclaim that object
  • Requirements:
    • Language must keep track of pointers that change or die (e.g., local variables when a function returns)
    • Language must also keep track of pointers within the heap itself when such pointers are either changed or their space is reclaimed.

Spring 2003 CS 403 Class Notes (^) Page 22

Reference Counts

  • May have to be modified:
    • When a function finishes and its variables die
    • Any time a pointer variable appears on the left-hand-side of an assignment statement
    • Any time the garbage collection routine reclaims a chunk of heap space containing a pointer
  • Reference counts don’t work on circular lists
    • Why not?

Spring 2003 CS 403 Class Notes (^) Page 23

Reference Counts

Mark and Sweep

  • Better than reference counts
  • Simplest form: 3 steps:
    • Collector walks through heap, marks each block

tentatively as useless

  • Beginning with each pointer outside the heap,

collector recursively explores all linked data

structures in the program, marking each newly

discovered block as useful.

  • Collector again walks through the heap again,

returning every useless block to free space

Spring 2003 CS 403 Class Notes (^) Page 25

Basic Idea: Mark and Sweep

Spring 2003 CS 403 Class Notes (^) Page 26

Problems with this Version

  • Non-fixed-size blocks:
    • Every block must start with:
      • An indication of its size
      • An indication of whether or not it is currently free
  • Collector must be able to find pointers

embedded within each block:

  • Some information at the beginning of the block

regarding where the internally embedded pointers

are

Spring 2003 CS 403 Class Notes (^) Page 27

Exam 1 Review

  • Tuesday; open-book, open-notes
  • Topics:
    • Compilation vs. interpretation
    • BNF; syntax specification
    • Storage models: static, stack, heap
    • Static and stack storage organization
    • Pointer design issues
    • Dangling pointers/memory leaks
    • Garbage collection
  • Question types:
    • Short answer
    • Write/evaluate segments of code
    • True/false (with justification)
    • Look at old exams on the Web