CS 61c: Great Ideas in Computer Architecture

Similar documents
Lecture 11 Doubly Linked Lists & Array of Linked Lists. Doubly Linked Lists

Lecture 10: Dynamic Memory Allocation 1: Into the jaws of malloc()

Memory Allocation. Static Allocation. Dynamic Allocation. Memory Management. Dynamic Allocation. Dynamic Storage Allocation

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Memory management. Announcements. Safe user input. Function pointers. Uses of function pointers. Function pointer example

Unix Security Technologies. Pete Markowsky <peterm[at] ccs.neu.edu>

ECS 165B: Database System Implementa6on Lecture 2

Stacks. Linear data structures

Format string exploitation on windows Using Immunity Debugger / Python. By Abysssec Inc

How To Port A Program To Dynamic C (C) (C-Based) (Program) (For A Non Portable Program) (Un Portable) (Permanent) (Non Portable) C-Based (Programs) (Powerpoint)

The Advantages of Dan Grossman CSE303 Spring 2005, Lecture 25

C++ INTERVIEW QUESTIONS

1 The Java Virtual Machine

How To Understand How A Process Works In Unix (Shell) (Shell Shell) (Program) (Unix) (For A Non-Program) And (Shell).Orgode) (Powerpoint) (Permanent) (Processes

C++ Programming Language

CS5460: Operating Systems

Building Embedded Systems

Static Code Analysis Procedures in the Development Cycle

CSC230 Getting Starting in C. Tyler Bletsch

Object Oriented Software Design II

8.5. <summary> Cppcheck addons Using Cppcheck addons Where to find some Cppcheck addons

MPLAB TM C30 Managed PSV Pointers. Beta support included with MPLAB C30 V3.00

Semantic Analysis: Types and Type Checking

An Introduction to Assembly Programming with the ARM 32-bit Processor Family

Lecture Data Types and Types of a Language

Linux/UNIX System Programming. POSIX Shared Memory. Michael Kerrisk, man7.org c February 2015

C Dynamic Data Structures. University of Texas at Austin CS310H - Computer Organization Spring 2010 Don Fussell

Get the Better of Memory Leaks with Valgrind Whitepaper

A C Test: The 0x10 Best Questions for Would-be Embedded Programmers

How To Write Portable Programs In C

Software security. Buffer overflow attacks SQL injections. Lecture 11 EIT060 Computer Security

Software Development Tools for Embedded Systems. Hesen Zhang

The C Programming Language course syllabus associate level

Sources: On the Web: Slides will be available on:

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

Oracle Solaris Studio Code Analyzer

Applying Clang Static Analyzer to Linux Kernel

Purify User s Guide. Version 4.1 support@rational.com

Jonathan Worthington Scarborough Linux User Group

1 Abstract Data Types Information Hiding

Introduction to Data Structures

Virtual Servers. Virtual machines. Virtualization. Design of IBM s VM. Virtual machine systems can give everyone the OS (and hardware) that they want.

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

Linked Lists, Stacks, Queues, Deques. It s time for a chainge!

Stack machines The MIPS assembly language A simple source language Stack-machine implementation of the simple language Readings:

Stack Allocation. Run-Time Data Structures. Static Structures

Chapter 7 Memory Management

Leak Check Version 2.1 for Linux TM

Software Vulnerabilities

3F6 - Software Engineering and Design. Handout 10 Distributed Systems I With Markup. Steve Young

Crash Course in Java

Short Notes on Dynamic Memory Allocation, Pointer and Data Structure

The V8 JavaScript Engine

Scoping (Readings 7.1,7.4,7.6) Parameter passing methods (7.5) Building symbol tables (7.6)

Logistics. Software Testing. Logistics. Logistics. Plan for this week. Before we begin. Project. Final exam. Questions?

Embedded Programming in C/C++: Lesson-1: Programming Elements and Programming in C

An Implementation of a Tool to Detect Vulnerabilities in Coding C and C++

Comp151. Definitions & Declarations

5 Arrays and Pointers

C# and Other Languages

Topics. Introduction. Java History CS 146. Introduction to Programming and Algorithms Module 1. Module Objectives

Illustration 1: Diagram of program function and data flow

Last Class: OS and Computer Architecture. Last Class: OS and Computer Architecture

Defending Computer Networks Lecture 3: More On Vulnerabili3es. Stuart Staniford Adjunct Professor of Computer Science

CSE 373: Data Structure & Algorithms Lecture 25: Programming Languages. Nicki Dell Spring 2014

Lecture 22: C Programming 4 Embedded Systems

language 1 (source) compiler language 2 (target) Figure 1: Compiling a program

Designing with Exceptions. CSE219, Computer Science III Stony Brook University

A deeper look at Inline functions

Java's garbage-collected heap

RTI Monitoring Library Getting Started Guide

Operating Systems. Privileged Instructions

C Interview Questions

Module 816. File Management in C. M. Campbell 1993 Deakin University

Off-by-One exploitation tutorial

System Software Prof. Dr. H. Mössenböck

System Calls and Standard I/O

Tail call elimination. Michel Schinz

Keil C51 Cross Compiler

Parallelization: Binary Tree Traversal

Object Oriented Software Design II

Unit Storage Structures 1. Storage Structures. Unit 4.3

Organization of Programming Languages CS320/520N. Lecture 05. Razvan C. Bunescu School of Electrical Engineering and Computer Science

Parallel and Distributed Computing Programming Assignment 1

Cryptography and Network Security Prof. D. Mukhopadhyay Department of Computer Science and Engineering

Time Limit: X Flags: -std=gnu99 -w -O2 -fomitframe-pointer. Time Limit: X. Flags: -std=c++0x -w -O2 -fomit-frame-pointer - lm

CHAPTER 4 ESSENTIAL DATA STRUCTRURES

Performance Improvement In Java Application

ELEC 377. Operating Systems. Week 1 Class 3

MatrixSSL Porting Guide

OPERATING SYSTEM - MEMORY MANAGEMENT

Phys4051: C Lecture 2 & 3. Comment Statements. C Data Types. Functions (Review) Comment Statements Variables & Operators Branching Instructions

Sample CSE8A midterm Multiple Choice (circle one)

Coding Rules. Encoding the type of a function into the name (so-called Hungarian notation) is forbidden - it only confuses the programmer.

Client-server Sockets

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

ASCII Encoding. The char Type. Manipulating Characters. Manipulating Characters

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Chapter 5 Names, Bindings, Type Checking, and Scopes

Programming Embedded Systems

Transcription:

Memory Management in C June 26, 2014

Review Arrays Can traverse using pointer or array syntax Use null-terminated char[] for strings Pointer arithmetic moves the pointer by the size of the thing it s pointing to No need for the programmer to worry about it

Great Idea #1: Levels of Representation/Interpretation High Level Language Program (e.g.c) Compiler Assembly Language Program (e.g. MIPS) Assembler Machine Language Program temp = v[k]; v[k] = v[k+1] v[k+1] = temp; lw $t0, 0($2) lw $t1, 4($2) sw $t1, 0($2) sw $t0, 4($2) 1000 1100 0100 1000 0000 0000 0000 0000 1000 1100 0100 1001 0000 0000 0000 0100 1010 1100 0100 1001 0000 0000 0000 0000 1010 1100 0100 1000 0000 0000 0000 0100 Machine Interpretation Hardware Architecture Description (e.g. block diagrams) Architecture Implementation Logic Circuit Description (Circuit Schematic Diagrams)

Outline Memory Layout In C Stack Mem Static and Code Data Administrivia Dynamic Memory Allocation Heap Common Problems Memory Management C Wrap-up Linked List Example

In C Memory Layout Program s address space contains 4 regions: Stack: local variables, grows downward Heap: Space requested via malloc(), grows upward Static Data: Global and static variables. Does not change size. Code: Loaded when program starts, does not change OS responsible for detecting accesses to unallocated region.

In C Which variables go where Static: Declared outside a function Stack: Declared inside a function note: main() is a function Freed on function return Heap: Dynamically allocated (e.g. with malloc()) # include <stdio.h> int varglobal; int main () { int varlocal; int *vardyn = malloc(sizeof(int));

Stack Mem The Stack Each stack frame is a contiguous block of memory holding the local variables of a single procedure A stack frame includes: Location of caller function Function arguments Space for local variables Stack pointer (SP) tells where the lowest (current) stack frame is When a function returns its stack frame is thrown out, freeing memory for future function calls SP Memory: frame frame frame

Stack Mem An Example Last in, First out (LIFO) data structure int main() { a(0); return 1; void a(int m) { b(1); SP The Stack: MAIN void b(int n) { c(2); d(4); void c(int o) { printf("c"); void d(int p) { printf("d");

Stack Mem An Example Last in, First out (LIFO) data structure int main() { a(0); return 1; void a(int m) { b(1); void b(int n) { c(2); d(4); SP The Stack: MAIN A void c(int o) { printf("c"); void d(int p) { printf("d");

Stack Mem An Example Last in, First out (LIFO) data structure int main() { a(0); return 1; void a(int m) { b(1); The Stack: MAIN A void b(int n) { c(2); d(4); void c(int o) { printf("c"); SP B void d(int p) { printf("d");

Stack Mem An Example Last in, First out (LIFO) data structure int main() { a(0); return 1; void a(int m) { b(1); The Stack: MAIN A void b(int n) { c(2); d(4); B void c(int o) { printf("c"); void d(int p) { printf("d"); SP C

Stack Mem An Example Last in, First out (LIFO) data structure int main() { a(0); return 1; void a(int m) { b(1); The Stack: MAIN A void b(int n) { c(2); d(4); void c(int o) { printf("c"); SP B void d(int p) { printf("d");

Stack Mem An Example Last in, First out (LIFO) data structure int main() { a(0); return 1; void a(int m) { b(1); The Stack: MAIN A void b(int n) { c(2); d(4); B void c(int o) { printf("c"); void d(int p) { printf("d"); SP D

Stack Mem An Example Last in, First out (LIFO) data structure int main() { a(0); return 1; void a(int m) { b(1); The Stack: MAIN A void b(int n) { c(2); d(4); void c(int o) { printf("c"); SP B void d(int p) { printf("d");

Stack Mem An Example Last in, First out (LIFO) data structure int main() { a(0); return 1; void a(int m) { b(1); void b(int n) { c(2); d(4); SP The Stack: MAIN A void c(int o) { printf("c"); void d(int p) { printf("d");

Stack Mem An Example Last in, First out (LIFO) data structure int main() { a(0); return 1; void a(int m) { b(1); SP The Stack: MAIN void b(int n) { c(2); d(4); void c(int o) { printf("c"); void d(int p) { printf("d");

Stack Mem Stack Misuse Never return pointers to locally allocated memory, e.g. int * getptr () { int y = 3; return &y; Compiler will warn you if you do this, don t ignore it Things can get really wonky if you do this (Boardwork): int main () { int * stackaddr, content ; stackaddr = getptr (); content = * stackaddr ; printf ("%d", content \n); /* 3 */ content = * stackaddr ; printf ("%d", content \n); /* -1216751336 */

Static and Code Data Static and Code Static: Place for variables that persist Good for data that never expands, shrinks, or goes stale E.g. String literals, global variables Size is constant, but contents can be modified Code: Where the executable data is stored We can represent anything with bits, including programs. More on how to do that later Does not change size Contents usually not allowed to be modified

Static and Code Data Question: Which statement below is FALSE? All statements assume that each variable exists. void funca (){ int x; printf ("A" ); void funcb () { int y; printf ("B"); funca (); void main () { char *s = "s"; funcb (); (blue) x is at a lower address than y (green) x and y are in adjacent stack frames (purple) x is at a lower address than *s (yellow) y is in the 2nd frame from the top of the Stack

Static and Code Data Question: Which statement below is FALSE? All statements assume that each variable exists. void funca (){ int x; printf ("A" ); void funcb () { int y; printf ("B"); funca (); void main () { char *s = "s"; funcb (); (blue) x is at a lower address than y (green) x and y are in adjacent stack frames (purple) x is at a lower address than *s (yellow) y is in the 2nd frame from the top of the Stack

Outline Memory Layout In C Stack Mem Static and Code Data Administrivia Dynamic Memory Allocation Heap Common Problems Memory Management C Wrap-up Linked List Example

Administrivia HW1 still due Sunday Project 1 released Start early! Start early! Did I mention to start early? You should start early.

Outline Memory Layout In C Stack Mem Static and Code Data Administrivia Dynamic Memory Allocation Heap Common Problems Memory Management C Wrap-up Linked List Example

Heap Dynamic Memory Allocation Sometimes you don t know how much memory you need beforehand e.g. input files, user input Dynamically allocated memory goes on the heap more permanent than the stack Needs as much space as possible without interfering with the stack this is why we start the stack at the top of memory, and the heap towards the bottom

Heap Allocating Memory Three functions for requisition memory: malloc(), calloc(), realloc() malloc(n) Allocates a contiguous block of n BYTES of uninitialized memory. Returns a pointer to the beginning of the allocated block; NULL if the request failed. Different blocks not necessarily adjacent

Heap Using malloc Almost always used for arrays or structs Good practice to use sizeof when allocating int *p = malloc (n * sizeof ( int )); Without the sizeof your code won t be very portable at all. Can use array or pointer syntax to access DON T lose the original address p++ is a terrible idea if p was malloc() d

Heap Releasing Memory Release memory on the heap using free() Memory is limited, should free when finished with it free(p) Releases the whole block that p pointed to p must point to the base of a malloc() d block Illegal to call free() on a block more than once

Heap Dynamic Memory Example Need #include <stdlib.h> typedef struct { int x; int y; point ; point * rect ; /* 2 opposite corners = rectangle */... rect = malloc (2* sizeof ( point )); /* Check malloc */ if (! rect ) { printf (" Out of memory!\n"); exit (1); /* Do NOT change rect in this region */...... free ( rect );

Heap Question: We want the output a[] = {0,1,2 with no errors. Which lines do we need to change? 1 # define N 3 2 int * makearray ( int n) { 3 int * arr ; 4 ar = ( int *) malloc (n); 5 return arr ; (blue) 4, 12 6 (green) 5, 12 7 int main () { (purple) 4,10 8 int i, *a = makearray (N); (yellow) 5, 10 9 for (i =0; i<n; i ++) 10 *a++ = i; 11 printf ("a[] = {%d,%d,%d",a[0],a[1],a [2]); 12 free (a); 13

Heap Question: We want the output a[] = {0,1,2 with no errors. Which lines do we need to change? 1 # define N 3 2 int * makearray ( int n) { 3 int * arr ; 4 ar = ( int *) malloc (n); 5 return arr ; (blue) 4, 12 6 (green) 5, 12 7 int main () { (purple) 4,10 8 int i, *a = makearray (N); (yellow) 5, 10 9 for (i =0; i<n; i ++) 10 *a++ = i; 11 printf ("a[] = {%d,%d,%d",a[0],a[1],a [2]); 12 free (a); 13

Common Problems Know Your Memory Errors 1 Segmentation Fault (more common in 61c) An error in which a running Unix program attempts to access memory not allocated to it and terminates with a segmentation violation error and usually a core dump Bus error (less common in 61c) A fatal failure in the execution of a machine language instruction resulting from the processor detecting an anomalous condition on its bus. Such conditions include invalid address alignment (accessing a multi-byte number at an odd address), accessing a physical address that does not correspond to any device, or some other device-specific hardware error. 1 Definitions from http://www.hyperdictionary.com

Common Problems Common Problems Using uninitialized values Using memory that you don t own Using NULL or garbage data as a pointer De-allocated stack or heap variable Out of bounds reference to stack or heap array Using memory you haven t allocated Freeing invalid memory Memory leaks

Common Problems Using Uninitialized Values What is wrong with this code? void foo ( int *p) { int j; *p = j; void bar () { int i = 10; foo (&i); printf ("i = %d\n", i);

Common Problems Using Uninitialized Values What is wrong with this code? void foo ( int *p) { int j; *p = j; // j is garbage void bar () { int i = 10; foo (&i); // i now contains garbage printf ("i = %d\n", i); // printing garbage

Common Problems Memory You Don t Own I What is wrong with this code? typedef struct node { struct node * next ; int val ; node ; int f indlas tnode Value ( node * head ) { while (head -> next ) head = head -> next ; return head -> val ;

Common Problems Memory You Don t Own I What is wrong with this code? typedef struct node { struct node * next ; int val ; node ; // What if head is NULL? int f indlas tnode Value ( node * head ) { // Segfault here! while (head -> next ) head = head -> next ; return head -> val ;

Common Problems Memory You Don t Own II What is wrong with this code? char * append ( const char * s1, const char * s2) { const int MAXSIZE = 128; char result [ MAXSIZE ]; int i = 0, j = 0; for (j =0; i<maxsize -1 && j< strlen (s1 ); i++,j ++) result [i] = s1[j]; for (j =0; i<maxsize -1 && j< strlen (s2 ); i++,j ++) result [i] = s2[j]; result [i] = \0 ; return result ;

Common Problems Memory You Don t Own II What is wrong with this code? char * append ( const char * s1, const char * s2) { const int MAXSIZE = 128; char result [ MAXSIZE ]; // local array is on stack int i = 0, j = 0; for (j =0; i<maxsize -1 && j< strlen (s1 ); i++,j ++) result [i] = s1[j]; for (j =0; i<maxsize -1 && j< strlen (s2 ); i++,j ++) result [i] = s2[j]; result [i] = \0 ; // return value no longer valid after we return! return result ;

Common Problems Memory You Don t Own III What is wrong with this code? typedef struct { char * name ; int age ; profile ; profile * person = malloc ( sizeof ( profile )); char * name = getname (); person -> name = malloc ( sizeof ( char ) * strlen ( name )); strcpy ( person ->name, name );... /* Do non - buggy stuff */ free ( person ); free ( person -> name );

Common Problems Memory You Don t Own III What is wrong with this code? typedef struct { char * name ; int age ; profile ; profile * person = malloc ( sizeof ( profile )); char * name = getname (); // No space for the null terminator person -> name = malloc ( sizeof ( char ) * strlen ( name )); strcpy ( person ->name, name );... free ( person ); // Oops, person was just deallocated, should // have done this first free ( person -> name );

Common Problems Memory You Haven t Allocated I What is wrong with this code? void str_ manip () { const char * name = " Safety Critical "; char * str = malloc (10); strncpy ( str, name, 10); str [10] = \0 ; printf ("%s\n", str );

Common Problems Memory You Haven t Allocated I What is wrong with this code? void str_ manip () { const char * name = " Safety Critical "; char * str = malloc (10); strncpy ( str, name, 10); str [10] = \0 ; // Out of bounds write printf ("% s\ n", str ); // Out of bounds read

Common Problems Memory You Haven t Allocated II What is wrong with this code? char buffer [1024]; int main ( int argc, char * argv []) { strcpy ( buffer, argv [1]);...

Common Problems Memory You Haven t Allocated II What is wrong with this code? char buffer [1024]; int main ( int argc, char * argv []) { // What if strlen ( argv [1]) > 1023? strcpy ( buffer, argv [1]);...

Common Problems Freeing Invalid Memory What is wrong with this code? void free_ memx () { int fnh = 0; free (& fnh ); void free_ memy () { int * fum = malloc (4 * sizeof ( int )); free ( fum + 1); free ( fum ); free ( fum );

Common Problems Freeing Invalid Memory What is wrong with this code? void free_ memx () { int fnh = 0; free (& fnh ); // Not heap allocated void free_ memy () { int * fum = malloc (4 * sizeof ( int )); free ( fum + 1); // Does not point to start of block free ( fum ); free ( fum ); // Double - free

Common Problems Memory Leaks I What is wrong with this code? int *pi; void foo () { pi = malloc (8 * sizeof ( int ));... free (pi ); void main () { pi = malloc (4* sizeof ( int )); foo ();

Common Problems Memory Leaks I What is wrong with this code? int *pi; void foo () { // Overwrites old pointer // 4* sizeof ( int ) bytes from main leaked pi = malloc (8 * sizeof ( int ));... free (pi ); void main () { pi = malloc (4* sizeof ( int )); foo ();

Common Problems Debugging Tools Runtime analysis tools for finding memory errors Dynamic analysis tool: collects information on memory management while program runs Doesn t work to find ALL memory bugs (this is an incredibly challenging problem), but will detect leaks for you You ll be using valgrind in lab 4, and on your project to check for memory leaks.

Common Problems Technology Break

Memory Management Memory Management Many calls to malloc() and free() with many different size blocks where are they placed? Want system to be fast with minimal memory overhead In contrast to an automatic garbage collection system, like in Java or Python Want to avoid fragmentation, the tendency of available memory to get separated into small chunks

Memory Management Fragmentation Example. 1. Block 1: malloc(100) B1 Heap

Memory Management Fragmentation Example. 1. Block 1: malloc(100) 2. Block 2: malloc(1) B2 (1 byte) B1 Heap

Memory Management Fragmentation Example. 1. Block 1: malloc(100) 2. Block 2: malloc(1) 3. Block 1: free() B2 (1 byte) Heap

Memory Management Fragmentation Example. 1. Block 1: malloc(100) 2. Block 2: malloc(1) 3. Block 1: free() 4. Block 3: malloc(50) Note, could go above B2 B2 (1 byte) B3 Heap

Memory Management Fragmentation Example. 1. Block 1: malloc(100) 2. Block 2: malloc(1) 3. Block 1: free() 4. Block 3: malloc(50) Note, could go above B2 5. Block 4: malloc(60) B4 B3 Heap B2 (1 byte)

Memory Management Basic Allocation Strategy: K&R Section 8.7 offers an implementation of memory managment (linked list of free blocks) This is just one of many possible memory management algorithms Just to give you a taste No single best approach for every application

Memory Management K&R Implementation Each block holds its own size and a pointer to the next block free() adds block to the list, combines with adjacent free blocks malloc() searches free list for block large enough to meet request If multiple blocks fit request, which one do we use?

Memory Management Choosing a Block Best-fit: Choose smallest block that fits request Tries to limit wasted fragmentation space, but takes more time and leaves a lot of small blocks First-fit: Choose first block that is large enough (always starts from the beginning) Fast, but tends to concentrate small blocks at the beginning Next-fit: Like first-fit, but resume search from where we last left off Fast, and does not concentrate small blocks at front

Memory Management Question: Which allocation system and set of requests will create a contiguous allocated region in the Heap? B3 was the last fulfilled request. 50 (blue) Best-fit:malloc(50), malloc(50) (green) First-fit:malloc(50), malloc(30) (purple) Next-fit:malloc(30), malloc(50) (yellow) Next-fit:malloc(50), malloc(30) 10 B2 30 10 B3 50 50 B1

Memory Management Question: Which allocation system and set of requests will create a contiguous allocated region in the Heap? B3 was the last fulfilled request. 50 (blue) Best-fit:malloc(50), malloc(50) (green) First-fit:malloc(50), malloc(30) (purple) Next-fit:malloc(30), malloc(50) (yellow) Next-fit:malloc(50), malloc(30) 10 B2 30 10 B3 50 50 B1

Outline Memory Layout In C Stack Mem Static and Code Data Administrivia Dynamic Memory Allocation Heap Common Problems Memory Management C Wrap-up Linked List Example

Linked List Example Linked List Example We want to generate a linked list of strings This example uses structs, pointers, malloc(), and free() First, we ll need a structure for list nodes typedef struct node { char * value ; struct node * next ; node ;

Linked List Example Adding a node to the list char *s1 = " start ", *s2 = " middle "; char *s3 = " end "; node * list = NULL ; /* Creates the list {" start, " middle ", " end " */ list = prepend ( s3, list ); list = prepend ( s2, list ); list = prepend ( s1, list );

Linked List Example Adding a node ot the list Let s examine the 3rd call ("start"): node * prepend ( char *s, node * lst ) { node * node = malloc ( sizeof ( node )); node -> value = malloc ( strlen (s) + 1); strcpy (node -> value, s); node -> next = lst ; return node ;

Linked List Example Adding a node ot the list Let s examine the 3rd call ("start"): node * prepend ( char *s, node * lst ) { node * node = malloc ( sizeof ( node )); node -> value = malloc ( strlen (s) + 1); strcpy (node -> value, s); node -> next = lst ; return node ; Boardwork!

Linked List Example Removing a node Now let s remove "start" from the list: node * del_front ( node * lst ) { node * tmp = lst -> next ; free (lst -> value ); free ( lst ); return tmp ;

Linked List Example Removing a node Now let s remove "start" from the list: node * del_front ( node * lst ) { node * tmp = lst -> next ; free (lst -> value ); free ( lst ); return tmp ; Boardwork!

Linked List Example Additional Functionality How might you implement the following (left as exercises to the reader): Append node to end of a list Delete/free an entire list Join two lists together Sort a list

Linked List Example Summary C memory layout Static Data: globals and string literals Code: copy of machine code Stack: local variables Heap: dynamic storage via malloc() and free() Memory management Want fast, with minimal fragmentation