Lecture 18: Deep C Garbage Collection CS201j: Engineering Software

Slides:



Advertisements
Similar presentations
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 20: Hair Dryer Attacks Image.
Advertisements

Dynamic Memory Management
SPLINT STATIC CHECKING TOOL Sripriya Subramanian 10/29/2002.
Lecture 10: Heap Management CS 540 GMU Spring 2009.
Kernighan/Ritchie: Kelley/Pohl:
Memory allocation CSE 2451 Matt Boggus. sizeof The sizeof unary operator will return the number of bytes reserved for a variable or data type. Determine:
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 21: Garbage Collection.
Joel Winstead CS201j: Engineering Software University of Virginia Computer Science Lecture 16: Pointers and Memory Management.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 18.
CPSC 388 – Compiler Design and Construction
CS 536 Spring Automatic Memory Management Lecture 24.
David Notkin Autumn 2009 CSE303 Lecture 10 "If it weren't for C, we'd be writing programs in BASI, PASAL, and OBOL."
C and Data Structures Baojian Hua
CS 61C L03 C Arrays (1) A Carle, Summer 2005 © UCB inst.eecs.berkeley.edu/~cs61c/su05 CS61C : Machine Structures Lecture #3: C Pointers & Arrays
Memory Layout C and Data Structures Baojian Hua
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 22: Low-Level Programming in.
CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans Lecture 10: *&!%[]++
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 18: 0xCAFEBABE (Java Byte Codes)
Dynamic Memory Allocation Questions answered in this lecture: When is a stack appropriate? When is a heap? What are best-fit, first-fit, worst-fit, and.
Variables and Objects, pointers and addresses: Chapter 3, Slide 1 variables and data objects are data containers with names the value of the variable is.
IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education.
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 17: Garbage Collection.
Runtime Environments. Support of Execution  Activation Tree  Control Stack  Scope  Binding of Names –Data object (values in storage) –Environment.
Object-Oriented Programming in C++
1 Homework HW5 due today Review a lot of things about allocation of storage that may not have been clear when we covered them in our initial pass Introduction.
Pointers *, &, array similarities, functions, sizeof.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 10 – C: the heap and manual memory management.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 9: Designing Exceptionally.
CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans Lecture 12: Automating Memory Management
C Tutorial - Pointers CS 537 – Introduction to Operating Systems.
Runtime Environments Chapter 7. Support of Execution  Activation Tree  Control Stack  Scope  Binding of Names –Data object (values in storage) –Environment.
Windows Programming Lecture 03. Pointers and Arrays.
Variables Bryce Boe 2012/09/05 CS32, Summer 2012 B.
Yan Shi CS/SE2630 Lecture Notes
CSE 220 – C Programming malloc, calloc, realloc.
Object Lifetime and Pointers
CMSC 341 Lecture 2 – Dynamic Memory and Pointers (Review)
CS1010 Programming Methodology
Computer Organization and Design Pointers, Arrays and Strings in C
CSE 374 Programming Concepts & Tools
What the (Pointers in Rust)
Lecture 15: Using Low-Level Languages CS201j: Engineering Software
COM S 326X Deep C Programming for the 21st Century Prof. Rozier
Checking Memory Management
CSCI206 - Computer Organization & Programming
Programming Languages and Paradigms
Advanced Programming Behnam Hatami Fall 2017.
Clear1 and Clear2 clear1(int array[], int size) { int i; for (i = 0; i < size; i += 1) array[i] = 0; } clear2(int *array, int size) {
CSC 253 Lecture 8.
Storage.
Java Byte Codes (0xCAFEBABE) cs205: engineering software
CSC 253 Lecture 8.
Instructor: Ioannis A. Vetsikas
Dynamic Memory Allocation
Memory Allocation CS 217.
Pointers C#, pointers can only be declared to hold the memory addresses of value types int i = 5; int *p; p = &i; *p = 10; // changes the value of i to.
Dynamic Memory A whole heap of fun….
Lecture 4: Data Abstraction CS201j: Engineering Software
CS111 Computer Programming
Pointers Pointers point to memory locations
Dynamic Memory A whole heap of fun….
Java Programming Language
CS216: Program and Data Representation
Chapter 9: Pointers and String
Class 24: Garbage Collection
SPL – PS2 C++ Memory Handling.
Presentation transcript:

David Evans http://www.cs.virginia.edu/evans Lecture 18: Deep C Garbage Collection CS201j: Engineering Software University of Virginia Computer Science David Evans http://www.cs.virginia.edu/evans

Menu Pointers in C Type checking in C Pointer Arithmetic Type checking in C Why is garbage collection hard in C? 7 November 2002 CS 201J Fall 2002

What are those arrows really? Stack Heap sb “hello” 7 November 2002 CS 201J Fall 2002

Pointers In Java, an object reference is really just an address in memory But Java doesn’t let programmers manipulate addresses directly Heap Stack 0x80496f0 0x80496f4 0x80496f8 hell 0x80496fb o\0\0\0 sb 0x80496f8 0x8049700 0x8049704 0x8049708 7 November 2002 CS 201J Fall 2002

Pointers in C &expr Evaluates to the address of the Addresses in memory Programs can manipulate addresses directly &expr Evaluates to the address of the location expr evaluates to *expr Evaluates to the value stored in the address expr evaluates to 7 November 2002 CS 201J Fall 2002

&*%&@#*! s == 1, t == 1 s == 2, t == 1 s == 3, t == 1 s == 3, t == 3 int f (void) { int s = 1; int t = 1; int *ps = &s; int **pps = &ps; int *pt = &t; **pps = 2; pt = ps; *pt = 3; t = s; } s == 1, t == 1 s == 2, t == 1 s == 3, t == 1 s == 3, t == 3 7 November 2002 CS 201J Fall 2002

Rvalues and Lvalues What does = really mean? int f (void) { int s = 1; int t = 1; t = s; t = 2; } left side of = is an “lvalue” it evaluates to a location (address)! right side of = is an “rvalue” it evaluates to a value There is an implicit * when a variable is used as an rvalue! 7 November 2002 CS 201J Fall 2002

BLISS Aside BLISS [Wulf71] Made getting values explicit s = .t; Puts the value in the location t in the location s 7 November 2002 CS 201J Fall 2002

The value of i (3) is passed, not its location! Parameter Passing in C Actual parameters are rvalues void swap (int a, int b) { int tmp = b; b = a; a = tmp; } int main (void) { int i = 3; int j = 4; swap (i, j); … The value of i (3) is passed, not its location! swap does nothing 7 November 2002 CS 201J Fall 2002

The value of &i is passed, which is the address of i Parameter Passing in C Can pass addresses around void swap (int *a, int *b) { int tmp = *b; *b = *a; *a = tmp; } int main (void) { int i = 3; int j = 4; swap (&i, &j); … The value of &i is passed, which is the address of i 7 November 2002 CS 201J Fall 2002

Beware! *ip == 3 *ip == 35 int *value (void) { > splint value.c int i = 3; return &i; } void callme (void) int x = 35; int main (void) { int *ip; ip = value (); printf (“*ip == %d\n", *ip); callme (); printf ("*ip == %d\n", *ip); > splint value.c Splint 3.0.1.7 --- 08 Aug 2002 value.c: (in function value) value.c:4:10: Stack-allocated storage &i reachable from return value: &i A stack reference is pointed to by an external reference when the function returns. The stack-allocated storage is destroyed after the call, leaving a dangling reference. (Use -stackref to inhibit warning) … But it could really be anything! *ip == 3 *ip == 35 7 November 2002 CS 201J Fall 2002

Manipulating Addresses char s[6]; s[0] = ‘h’; s[1] = ‘e’; s[2]= ‘l’; s[3] = ‘l’; s[4] = ‘o’; s[5] = ‘\0’; printf (“s: %s\n”, s); expr1[expr2] in C is just syntactic sugar for *(expr1 + expr2) s: hello 7 November 2002 CS 201J Fall 2002

Obfuscating C char s[6]; *s = ‘h’; *(s + 1) = ‘e’; 2[s] = ‘l’; *(s + 4) = ‘o’; 5[s] = ‘\0’; printf (“s: %s\n”, s); s: hello 7 November 2002 CS 201J Fall 2002

Fun with Pointer Arithmetic int match (char *s, char *t) { int count = 0; while (*s == *t) { count++; s++; t++; } return count; } int main (void) { char s1[6] = "hello"; char s2[6] = "hohoh"; printf ("match: %d\n", match (s1, s2)); printf ("match: %d\n", match (s2, s2 + 2)); printf ("match: %d\n", match (&s2[1], &s2[3])); &s2[1] &(*(s2 + 1))  s2 + 1 The \0 is invisible! match: 1 match: 3 match: 2 7 November 2002 CS 201J Fall 2002

Condensing match int match (char *s, char *t) { int count = 0; while (*s == *t) { count++; s++; t++; } return count; } int match (char *s, char *t) { char *os = s; while (*s++ == *t++); return s – os - 1; } s++ evaluates to spre, but changes the value of s Hence, C++ has the same value as C, but has unpleasant side effects. 7 November 2002 CS 201J Fall 2002

Type Checking in C Java: only allow programs the compiler can prove are type safe C: trust the programmer. If she really wants to compare apples and oranges, let her. Exception: run-time type errors for downcasts and array element stores. 7 November 2002 CS 201J Fall 2002

(earlier versions of Windows would just crash the whole machine) Type Checking int main (void) { char *s = (char *) 3; printf ("s: %s", s); } Windows2000 (earlier versions of Windows would just crash the whole machine) 7 November 2002 CS 201J Fall 2002

In Praise of Type Checking int match (int *s, int *t) { int *os = s; while (*s++ == *t++); return s - os; } int main (void) { char s1[6] = "hello"; char s2[6] = "hello"; printf ("match: %d\n", match (s1, s2)); match: 2 7 November 2002 CS 201J Fall 2002

Different Matching different: 29 int different (int *s, int *t) { int *os = s; while (*s++ != *t++); return s - os; } int main (void) { char s1[6] = "hello"; printf ("different: %d\n", different ((int *)s1, (int *)s1 + 1)); different: 29 7 November 2002 CS 201J Fall 2002

So, why is it hard to garbage collect C? 7 November 2002 CS 201J Fall 2002

Mark and Sweep (Java version) active = all objects on stack while (!active.isEmpty ()) newactive = { } foreach (Object a in active) mark a as reachable foreach (Object o that a points to) if o is not marked newactive = newactive U { o } active = newactive sweep () // remove unmarked objects on heap 7 November 2002 CS 201J Fall 2002

Mark and Sweep (C version?) active = all pointers on stack while (!active.isEmpty ()) newactive = { } foreach (pointer a in active) mark *a as reachable foreach (address p that a points to) if *p is not marked newactive = newactive U { *p } active = newactive sweep () // remove unmarked objects on heap 7 November 2002 CS 201J Fall 2002

There may be objects that only have pointers to their middle! GC Challenges char *f (void) { char *s = (char *) malloc (sizeof (char) * 100); s = s + 20; *s = ‘a’; return s – 20; } There may be objects that only have pointers to their middle! 7 November 2002 CS 201J Fall 2002

GC Challenges char *f (void) { char *s = (char *) malloc (sizeof (char) * 100); int x = (int) s; s = 0; return (char *) x; } There may be objects that are reachable through values that have non-pointer apparent types! 7 November 2002 CS 201J Fall 2002

GC Challenges char *f (void) { char *s = (char *) malloc (sizeof (char) * 100); int x = (int) s; x = x - &f; s = 0; return (char *) (x + &f); } There may be objects that are reachable through values that have non-pointer apparent types and have values that don’t even look like addresses! 7 November 2002 CS 201J Fall 2002

Why not just do reference counting? Where can you store the references? Remember C programs can access memory directly, better not change how objects are stored! 7 November 2002 CS 201J Fall 2002

Summary Garbage collection depends on: Both of these are problems in C Knowing which values are addresses Knowing that objects without references cannot be reached Both of these are problems in C Nevertheless, there are some garbage collectors for C. Change meaning of some programs Slow down programs a lot Are not able to find all garbage 7 November 2002 CS 201J Fall 2002

Charge PS6 due Tuesday Exam 2 out Thursday Remaining classes: Send review questions if you want a review class Remaining classes: Java Byte Codes Security Concurrency without synchronization Project Management Garbage Collectors (COAX, Seoul, 18 June 2002) 7 November 2002 CS 201J Fall 2002