dynamic memory allocation

114
1 Dynamic Memory Allocation

Upload: auryon

Post on 23-Mar-2016

74 views

Category:

Documents


1 download

DESCRIPTION

Dynamic Memory Allocation. Outline. Implementation of a simple allocator Explicit Free List Segregated Free List Suggested reading: 10.9, 10.10, 10.11, 10.12, 10.13. Segregate: 隔离. Dynamic Memory Allocation P731. Explicit vs. Implicit Memory Allocator - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Dynamic Memory Allocation

1

Dynamic Memory Allocation

Page 2: Dynamic Memory Allocation

2

Outline

• Implementation of a simple allocator• Explicit Free List• Segregated Free List• Suggested reading: 10.9, 10.10, 10.11,

10.12, 10.13

Segregate:隔离

Page 3: Dynamic Memory Allocation

3

Dynamic Memory Allocation P731

• Explicit vs. Implicit Memory Allocator– Explicit: application allocates and frees space

• E.g., malloc and free in C

– Implicit: application allocates, but does not free space

• E.g. garbage collection in Java, ML or Lisp

Page 4: Dynamic Memory Allocation

4

Dynamic Memory Allocation

• Allocation– In both cases the memory allocator provides an

abstraction of memory as a set of blocks– Doles out free memory blocks to application

Doles: 发放

Page 5: Dynamic Memory Allocation

5

10.9.1 The malloc and free Functions

Page 6: Dynamic Memory Allocation

6

Malloc package P731

• #include <stdlib.h>• void *malloc(size_t size)

– if successful:• returns a pointer to a memory block of at least size bytes, aligned to

8-byte boundary.• if size==0, returns NULL

– if unsuccessful: returns NULL

• void free(void *p)– returns the block pointed at by p to pool of available memory– p must come from a previous call to malloc,calloc or realloc.

Page 7: Dynamic Memory Allocation

7

sbrk() Function P732

• #include <unistd.h>• void *sbrk(int incr)

– If successful• It returns the old value of brk

– If unsuccessful• It returns –1• It sets errno to ENOMEM

– If incr is zero• It returns the current value

– incr can be a negative number

Page 8: Dynamic Memory Allocation

8

• Assumptions made in this lecture– memory is word addressed (each word can

hold a pointer)

Allocated block(4 words)

Free block(3 words)

Free word

Allocated word

Assumptions

Page 9: Dynamic Memory Allocation

9

p1 = malloc(4)

p2 = malloc(5)

p3 = malloc(6)

free(p2)

p4 = malloc(2)

Allocation examples

Figure 10.36 P733

Page 10: Dynamic Memory Allocation

10

10.9.2 Why Dynamic Memory Allocation

Page 11: Dynamic Memory Allocation

11

1 #include "csapp.h"2 #define MAXN 1521334 int array[MAXN];56 int main()7 {8 int i, n;910 scanf("%d", &n);11 if (n > MAXN)12 app_error("Input file too big");13 for (i = 0; i < n; i++)14 scanf("%d", &array[i]);15 exit(0);16 }

Why Dynamic Memory Allocation P734

Page 12: Dynamic Memory Allocation

12

1 #include "csapp.h"23 int main()4 {5 int *array, i, n;67 scanf("%d", &n);8 array = (int *)Malloc(n * sizeof(int));9 for (i = 0; i < n; i++)10 scanf("%d", &array[i]);11 exit(0);12 }

Why Dynamic Memory Allocation P734

Page 13: Dynamic Memory Allocation

13

10.9.3 Allocator Requirements and Goals

Page 14: Dynamic Memory Allocation

14

Constraints

• Applications:– Can issue arbitrary sequence of allocation and

free requests– Free requests must correspond to an allocated

block

Page 15: Dynamic Memory Allocation

15

Constraints

• Allocators– Can’t control number or size of allocated blocks– Must respond immediately to all allocation

requests• i.e., can’t reorder or buffer requests

– Must allocate blocks from free memory• i.e., can only place allocated blocks in free memory

Page 16: Dynamic Memory Allocation

16

Constraints

• Allocators– Must align blocks so they satisfy all alignment

requirements• usually 8 byte alignment

– Can only manipulate and modify free memory– Can’t move the allocated blocks once they are

allocated• i.e., compaction is not allowed

Page 17: Dynamic Memory Allocation

17

Goals P735

• Given some sequence of malloc and free requests:– R0, R1, ..., Rk, ... , Rn-1

• Want to maximize throughput and peak memory utilization.– These goals are often conflicting

Page 18: Dynamic Memory Allocation

18

Performance goals: throughput

• Number of completed requests per unit time

• Example:– 5,00 malloc calls and 5,00 free calls in 1

seconds – throughput is 1,000 operations/second.

Page 19: Dynamic Memory Allocation

19

Performance goals: peak memory utilization

• Given some sequence of malloc and free requests:– R0, R1, ..., Rk, ... , Rn-1

• Def: aggregate payload Pk: – malloc(p) results in a block with a payload of p

bytes.– After request Rk has completed, the aggregate

payload Pk is the sum of currently allocated payloads.

Aggregate: 合计,累计

Page 20: Dynamic Memory Allocation

20

Performance goals: peak memory utilization

• Given some sequence of malloc and free requests:– R0, R1, ..., Rk, ... , Rn-1

• Def: current heap size is denoted by Hk

– Note that Hk is monotonically nondecreasing• Def: peak memory utilization:

– After k requests, peak memory utilization is:• Uk = ( maxi<k Pi ) / Hk

Page 21: Dynamic Memory Allocation

21

10.9.4 Fragmentation

Fragmentation: 分成碎片

Page 22: Dynamic Memory Allocation

22

Fragmentation

• Poor memory utilization caused by fragmentation– Comes in two forms:

• internal fragmentation• external fragmentation

Page 23: Dynamic Memory Allocation

23

Internal Fragmentation

• Internal fragmentation– For some block, internal fragmentation is the

difference between the block size and the payload size

payloadInternal fragmentation

block

Internal fragmentation

Page 24: Dynamic Memory Allocation

24

Internal Fragmentation

• Internal fragmentation– Is caused by overhead of maintaining heap

data structures, padding for alignment purposes, or explicit policy decisions (e.g., not to split the block).

– Depends only on the pattern of previous requests, and thus is easy to measure.

Page 25: Dynamic Memory Allocation

25

External fragmentation

• Occurs when there is enough aggregate heap memory, but no single

• free block is large enoughp1 = malloc(4)

p2 = malloc(5)

p3 = malloc(6)

free(p2)

p4 = malloc(6)

Page 26: Dynamic Memory Allocation

26

External fragmentation

• External fragmentation depends on – the pattern of future requests– and thus is difficult to measure

Page 27: Dynamic Memory Allocation

27

10.9.5 Implementation Issues

Page 28: Dynamic Memory Allocation

28

Implementation issues

• How do we know how much memory to free just given a pointer?

• How do we keep track of the free blocks?

p1 = malloc(1)

p0

free(p0)

Page 29: Dynamic Memory Allocation

29

Implementation issues

• What do we do with the extra space when allocating a structure that is smaller than the free block it is placed in?

• How do we pick a block to use for allocation – many might fit?

• How do we reinsert freed block?Reinsert:重新插入

Page 30: Dynamic Memory Allocation

30

Knowing how much to free

• Standard method– keep the length of a structure in the word

preceding the structure• This word is often called the header field or header

– requires an extra word for every allocated structure

Page 31: Dynamic Memory Allocation

31

Knowing how much to free

free(p0)

p0 = malloc(4) p0

Block size data

5

Page 32: Dynamic Memory Allocation

32

10.9.6 Implicit Free Lists

Implicit:暗示的,绝对的

Page 33: Dynamic Memory Allocation

33

Implicit list

• Need to identify whether each block is free or allocated– Can use extra bit– Bit can be put in the same word as the size if

block sizes are always multiples of 8 (mask out low order bit when reading size).

Page 34: Dynamic Memory Allocation

34

Implicit list

size

1 word

Format ofallocated andfree blocks

payload

a = 1: allocated block a = 0: free block

size: block size

payload: application data(allocated blocks only)

a

optionalpadding

00

4 4 26

p

Figure 10.38 P738

Figure 10.37 P738

Page 35: Dynamic Memory Allocation

35

10.9.7 Placing Allocated Blocks

Page 36: Dynamic Memory Allocation

36

Finding a free block

• 1 ) First fit:– Search list from beginning, choose first free

block that fits– Can take linear time in total number of blocks

(allocated and free)– In practice it can cause “splinters” at

beginning of listp = start; while ((p < end) || \\ not passed end (*p & 1) || \\ already allocated (*p <= len) ); \\ too small

Page 37: Dynamic Memory Allocation

37

Finding a free block

• 2 ) Next fit:– Like first-fit, but search list from location of

end of previous search– Research suggests that fragmentation is worse

• 3 ) Best fit:– Search the list, choose the free block with the

closest size that fits– Keeps fragments small --- usually helps

fragmentation– Will typically run slower than first-fit

Page 38: Dynamic Memory Allocation

38

10.9.8 Splitting Free Blocks

Page 39: Dynamic Memory Allocation

39

Allocating in a free block

• Allocating in a free block - splitting– Since allocated space might be smaller than

free space, we might want to split the block

4 4 26

p

4 24 24

Figure 10.39 P740

Page 40: Dynamic Memory Allocation

40

10.9.9 Getting Additional Heap Memory

Page 41: Dynamic Memory Allocation

41

10.9.10 Coalescing Free Blocks

Coalescing:接合

Page 42: Dynamic Memory Allocation

42

Freeing a block

• Simplest implementation:– Only need to clear allocated flag– But can lead to “false fragmentation” – There is enough free space, but the allocator

won’t be able to find it

4 24 2

free(p) p

4 4 2

4

4 2

malloc(5)

Page 43: Dynamic Memory Allocation

43

Coalescing

• Join with next and/or previous block if they are free– Coalescing with next block– But how do we coalesce with previous block?

4 24 2

free(p) p

4 4 2

4

6

Figure 10.40 P741

Page 44: Dynamic Memory Allocation

44

10.9.11 Coalescing with Boundary Tags

Page 45: Dynamic Memory Allocation

45

Bidirectional

• Boundary tags [Knuth73]– replicate size/allocated word at bottom of free

blocks– Allows us to traverse the “list” backwards, but

requires extra space– Important and general technique!

Page 46: Dynamic Memory Allocation

46

Bidirectional

4 4 4 4 6 46 4

size

1 word

Format ofallocated andfree blocks

payload andpadding

a = 1: allocated block a = 0: free block

size: block size

payload: application data(allocated blocks only)

a

size aboundary tag (footer)

header 00

00

Figure 10.41 P742

Page 47: Dynamic Memory Allocation

47

allocated

allocated

allocated

free

free

allocated

free

free

block beingfreed

Case 1 Case 2 Case 3 Case 4

Constant time coalescing

Figure 10.42 P743

Page 48: Dynamic Memory Allocation

48

m1 1

m1 1n 1

n 1m2 1

m2 1

m1 1

m1 1n 0

n 0m2 1

m2 1

Constant time coalescing (case 1)

Page 49: Dynamic Memory Allocation

49

m1 1

m1 1n+m2 0

n+m2 0

m1 1

m1 1n 1

n 1m2 0

m2 0

Constant time coalescing (case 2)

Page 50: Dynamic Memory Allocation

50

m1 0

m1 0n 1

n 1m2 1

m2 1

n+m1 0

n+m1 0m2 1

m2 1

Constant time coalescing (case 3)

Page 51: Dynamic Memory Allocation

51

m1 0

m1 0n 1

n 1m2 0

m2 0

n+m1+m2 0

n+m1+m2 0

Constant time coalescing (case 4)

Page 52: Dynamic Memory Allocation

52

10.9.12 Putting it Together: Implementing a Simple Allocator

Page 53: Dynamic Memory Allocation

53

1 int mm_init(void);2 void *mm_malloc(size_t size);3 void mm_free(void *bp);

Implementing a Simple Allocator

Page 54: Dynamic Memory Allocation

54

Data Structure

Figure 10.44 P745

Page 55: Dynamic Memory Allocation

55

1 #include "csapp.h"23 /* private global variables */4 static void *mem_start_brk; /* points to first byte of the heap */5 static void *mem_brk; /* points to last byte of the heap */6 static void *mem_max_addr; /* max virtual address for the heap */78 /*9 * mem_init - initializes the memory system model10 */11 void mem_init(int size)12 {13 mem_start_brk = (void *)Malloc(size); /* models available VM

*/14 mem_brk = mem_start_brk; /* heap is initially

empty */15 mem_max_addr = mem_start_brk + size; /* max VM address for

heap */16 }17

Initialize Figure 10.43 P745

Page 56: Dynamic Memory Allocation

56

18 /*19 * mem_sbrk - simple model of the the sbrk function.

Extends the heap20 * by incr bytes and returns the start address of the

new area. In21 * this model, the heap cannot be shrunk.22 */23 void *mem_sbrk(int incr)24 {25 void *old_brk = mem_brk;2627 if ( (incr < 0) || ((mem_brk + incr) > mem_max_addr)) {28 errno = ENOMEM;29 return (void *)-1;30 }31 mem_brk += incr;32 return old_brk;33 }

Initialize

Page 57: Dynamic Memory Allocation

57

1 /* Basic constants and macros */2 #define WSIZE 4 /* word size (bytes) */3 #define DSIZE 8 /* doubleword size (bytes) */4 #define CHUNKSIZE (1<<12) /* initial heap size (bytes) */5 #define OVERHEAD 8 /* overhead of header and footer (bytes)

*/67 #define MAX(x, y) ((x) > (y)? (x) : (y))89 /* Pack a size and allocated bit into a word */10 #define PACK(size, alloc) ((size) | (alloc))1112 /* Read and write a word at address p */13 #define GET(p) (*(size_t *)(p))14 #define PUT(p, val) (*(size_t *)(p) = (val))15

Macros Figure 10.45 P746

Page 58: Dynamic Memory Allocation

58

16 /* Read the size and allocated fields from address p */17 #define GET_SIZE(p) (GET(p) & ˜0x7)18 #define GET_ALLOC(p) (GET(p) & 0x1)1920 /* Given block ptr bp, compute address of its header and footer */21 #define HDRP(bp) ((void *)(bp) - WSIZE)22 #define FTRP(bp) ((void *)(bp) + GET_SIZE(HDRP(bp)) - DSIZE)2324 /* Given block ptr bp, compute address of next and previous blocks

*/25 #define NEXT_BLKP(bp) ((void *)(bp) + GET_SIZE(HDRP(bp)))26 #define PREV_BLKP(bp) ((void *)(bp) - GET_SIZE(((void *)(bp) -

DSIZE)))

Size_ t size = GET SIZE(HDRP(NEXT_BLKP(bp)));

Macros

Page 59: Dynamic Memory Allocation

59

1 int mm_init(void)2 {3 /* create the initial empty heap */4 if ((heap_listp = mem_sbrk(4*WSIZE)) == NULL)5 return -1;6 PUT(heap_listp, 0); /* alignment padding */7 PUT(heap_listp+WSIZE, PACK(OVERHEAD, 1)); /* prologue header */8 PUT(heap_listp+DSIZE, PACK(OVERHEAD, 1)); /* prologue footer */9 PUT(heap_listp+WSIZE+DSIZE, PACK(0, 1)); /* epilogue header */10 heap_listp += DSIZE;1112 /* Extend the empty heap with a free block of CHUNKSIZE

bytes */13 if (extend_heap(CHUNKSIZE/WSIZE) == NULL)14 return -1;15 return 0;16 }

mm_init() Figure 10.46 P747

Page 60: Dynamic Memory Allocation

60

1 static void *extend_heap(size_t words)2 {3 char *bp;4 size_t size;56 /* Allocate an even number of words to maintain alignment */7 size = (words % 2) ? (words+1) * WSIZE : words * WSIZE;8 if ((int)(bp = mem_sbrk(size)) < 0)9 return NULL;1011 /* Initialize free block header/footer and the epilogue

header */12 PUT(HDRP(bp), PACK(size, 0)); /* free block header */13 PUT(FTRP(bp), PACK(size, 0)); /* free block footer */14 PUT(HDRP(NEXT_BLKP(bp)), PACK(0, 1)); /* new

epilogue header */1516 /* Coalesce if the previous block was free */17 return coalesce(bp);18 }

mm_init() Figure 10.47 P748

Page 61: Dynamic Memory Allocation

61

1 void mm_free(void *bp)2 {3 size_t size = GET_SIZE(HDRP(bp));45 PUT(HDRP(bp), PACK(size, 0));6 PUT(FTRP(bp), PACK(size, 0));7 coalesce(bp);8 }9

mm_free() Figure 10.48 P749

Page 62: Dynamic Memory Allocation

62

10 static void *coalesce(void *bp)11 {12 size_t prev_alloc = GET_ALLOC(FTRP(PREV_BLKP(bp)));13 size_t next_alloc = GET_ALLOC(HDRP(NEXT_BLKP(bp)));14 size_t size = GET_SIZE(HDRP(bp));1516 if (prev_alloc && next_alloc) { /* Case 1 */17 return bp;18 }1920 else if (prev_alloc && !next_alloc) { /* Case 2 */21 size += GET_SIZE(HDRP(NEXT_BLKP(bp)));22 PUT(HDRP(bp), PACK(size, 0));23 PUT(FTRP(bp), PACK(size,0));24 return(bp);25 }26

mm_free()

Page 63: Dynamic Memory Allocation

63

27 else if (!prev_alloc && next_alloc) { /* Case 3 */28 size += GET_SIZE(HDRP(PREV_BLKP(bp)));29 PUT(FTRP(bp), PACK(size, 0));30 PUT(HDRP(PREV_BLKP(bp)), PACK(size, 0));31 return(PREV_BLKP(bp));32 }3334 else { /* Case 4 */35 size += GET_SIZE(HDRP(PREV_BLKP(bp))) +36 GET_SIZE(FTRP(NEXT_BLKP(bp)));37 PUT(HDRP(PREV_BLKP(bp)), PACK(size, 0));38 PUT(FTRP(NEXT_BLKP(bp)), PACK(size, 0));39 return(PREV_BLKP(bp));40 }41 }

mm_free() Figure 10.48 P749

Page 64: Dynamic Memory Allocation

64

1 void *mm_malloc (size_t size)2 {3 size_t asize; /* adjusted block size */4 size_t extendsize; /* amount to extend heap if no fit */5 char *bp;67 /* Ignore spurious requests */8 if (size <= 0)9 return NULL;1011 /* Adjust block size to include overhead and alignment

reqs. */12 if (size <= DSIZE)13 asize = DSIZE + OVERHEAD;14 else15 asize = DSIZE * ((size + (OVERHEAD) + (DSIZE-1)) /

DSIZE);16

mm_malloc() Figure 10.49 P750

Page 65: Dynamic Memory Allocation

65

17 /* Search the free list for a fit */18 if ((bp = find_fit(asize)) != NULL) {19 place (bp, asize);20 return bp;21 }2223 /* No fit found. Get more memory and place the

block */24 extendsize = MAX (asize, CHUNKSIZE) ;25 if ((bp = extend_heap (extendsize/WSIZE)) == NULL)26 return NULL;27 place (bp, asize);28 return bp;29 }

mm_malloc()

Page 66: Dynamic Memory Allocation

66

1. static void *find_fit(size_t asize)2. {3. void *bp ;4. 5. /* first fit search */6. for (bp = heap_listp; GET_SIZE(HDRP(bp)) > 0 ; bp =

NEXT_BLKP(bp) ) {7. if (!GET_ALLOC(HDRP(bp)) &&

(asize<=GET_SIZE(HDRP(bp)))) {8. return bp;9. }10. }11. return NULL; /*no fit */12. }

mm_alloc() problem 10.8

Page 67: Dynamic Memory Allocation

67

1. static void place(void *bp, size_t asize)2. {3. size_t csize = GET_SIZE(HDRP(bp)) ;4. 5. if ( (csize –asize) >= (DSIZE + OVERHEAD) ) {6. PUT(HDRP(bp), PACK(asize, 1)) ;7. PUT(FTRP(bp), PACK(asize, 1)) ;8. bp = NEXT_BLKP(bp) ;9. PUT(HDRP(bp), PACK(csize-asize, 0) ;10. PUT(FTRP(bp), PACK(csize-asize, 0) ;11. } else {12. PUT(HDRP(bp), PACK(csize, 1) ;13. PUT(FTRP(bp), PACK(csize, 1) ;14. }15. }

mm_alloc() problem 10.9

Page 68: Dynamic Memory Allocation

68

10.9.13 Explicit Free Lists

Page 69: Dynamic Memory Allocation

69

Explicit free lists

• Explicit list among the free blocks using pointers within the free blocks

• Use data space for link pointers– Typically doubly linked– Still need boundary tags for coalescing– It is important to realize that links are not

necessarily in the same order as the blocks

Page 70: Dynamic Memory Allocation

70

Explicit free lists

A B C

4 4 4 4 66 44 4 4Forward links

Back links

A B

C

Page 71: Dynamic Memory Allocation

71

Freeing with explicit free lists

• Where to put the newly freed block in the free list– LIFO (last-in-first-out) policy

• insert freed block at the beginning of the free list• pro: simple and constant time• con: studies suggest fragmentation is worse than

address ordered.

Page 72: Dynamic Memory Allocation

72

Freeing with explicit free lists

• Where to put the newly freed block in the free list– Address-ordered policy

• insert freed blocks so that free list blocks are always in address order

– i.e. addr(pred) < addr(curr) < addr(succ)• con: requires search• pro: studies suggest fragmentation is better than

LIFO

Page 73: Dynamic Memory Allocation

73

10.9.14 Segregated Free Lists

Page 74: Dynamic Memory Allocation

74

Segregated Storage

• Each size “class” has its own collection of blocks– Often have separate collection for every small

size (2,3,4,…)– For larger sizes typically have a collection for

each power of 2

Page 75: Dynamic Memory Allocation

75

Segregated Storage

1-2345-89-16

Page 76: Dynamic Memory Allocation

76

• Separate heap and free list for each size class

• No splitting• To allocate a block of size n:

– if free list for size n is not empty,• allocate first block on list (note, list can be implicit or

explicit)– if free list is empty,

• get a new page • create new free list from all blocks in page• allocate first block on list

– constant time

1) Simple segregated storage

Page 77: Dynamic Memory Allocation

77

• To free a block:– Add to free list– If page is empty, return the page for use by

another size (optional)

• Tradeoffs:– fast, but can fragment badly

Simple segregated storage

Page 78: Dynamic Memory Allocation

78

• Array of free lists, each one for some size class

2) Segregated fits

Page 79: Dynamic Memory Allocation

79

• To allocate a block of size n:– search appropriate free list for block of size m

> n– if an appropriate block is found:

• split block and place fragment on appropriate list (optional)

– if no block is found, try next larger class– repeat until block is found– if no blocks is found in all classes, try more

heap memory

Segregated fits

Page 80: Dynamic Memory Allocation

80

• To free a block:– coalesce and place on appropriate list

(optional)• Tradeoffs

– faster search than sequential fits (i.e., log time for power of two size classes)

– controls fragmentation of simple segregated storage

– coalescing can increase search times• deferred coalescing can help

Segregated fits

Page 81: Dynamic Memory Allocation

81

• A special case of segregated fits– Each size is power of 2

• Initialize– A heap of size 2m

3) Buddy Systems

Page 82: Dynamic Memory Allocation

82

• Allocate– Roundup to power of 2 such as 2k

– Find a free block of size 2j (k j m)– Split the block in half until j=k

• Each remaining half block (buddy) is placed on the appreciate free list

• Free– Continue coalescing with the free buddies

Buddy Systems

Page 83: Dynamic Memory Allocation

83

10.10 Garbage Collection

Page 84: Dynamic Memory Allocation

84

10.10.1 Garbage Collector Basics

Page 85: Dynamic Memory Allocation

85

• Nodes– Root Nodes– Heap Nodes

• Reachable• Not-reachable (garbage)

– Directed Edge

Garbage Collector’s View of Memory as a Directed Graph

Page 86: Dynamic Memory Allocation

86

• Maintain some representation of the reachability graph

• Periodically reclaim the unreachable nodes by freeing them and returning them to the free list

Garbage Collector

Page 87: Dynamic Memory Allocation

87

• Each reachable block is correctly identified as reachable

• Some unreachable nodes might be incorrectly identified as reachable

Conservative Garbage Collector

Page 88: Dynamic Memory Allocation

88

Garbage Collector

• On demand• Run as separate threads in parallel with the

application

A Bmalloc()Conservative

garbage collector

free()AC application program

Dynamic storage allocator

Figure 10.52 P756

Page 89: Dynamic Memory Allocation

89

10.10.2 Mark&Sweep Garbage Collectors

Page 90: Dynamic Memory Allocation

90

Functions

• prt isPtr(ptr p)• int blockMarked(ptr b)• int blockAllocated(ptr b)• void markBlock(ptr b)• int length(b)• void unmarkBlock(ptr b)• prt nextBlock(ptr b)

Page 91: Dynamic Memory Allocation

91

10.10.3 Conservative Mark&Sweep for C Programs

Page 92: Dynamic Memory Allocation

92

10.11 Common Memory-Related Bugs in C Programs

Page 93: Dynamic Memory Allocation

93

10.11.1 Dereferencing Bad Pointers

Page 94: Dynamic Memory Allocation

94

Example

• scanf(‘’%d”, &val);• scanf(‘’%d”, val);

Page 95: Dynamic Memory Allocation

95

10.11.2 Reading Uninitialized Memory

Page 96: Dynamic Memory Allocation

96

Error

• Assume heap memory is initialized to zero

Page 97: Dynamic Memory Allocation

97

10.11.3 Allowing Stack Buffer Overflows

Page 98: Dynamic Memory Allocation

98

Buffer Overflow Bug

void bufoverflow(){

char buf[64];

gets(buf);return;

}

Page 99: Dynamic Memory Allocation

99

10.11.4 Assuming that Pointers and the Objects they Point to Are the Same Size

Page 100: Dynamic Memory Allocation

100

Common Mistake

• Assume that pointers to objects are the same size as the objects they point to

Page 101: Dynamic Memory Allocation

101

10.11.5 Making Off-by-One Errors

Page 102: Dynamic Memory Allocation

102

Overwriting Bugs

int **makeArray2(int n, int m){

int I;int **A=(int **)Malloc(n*sizeof(int));

for (i=0; i<=n; i++)A[i] = (int *)Malloc(m*sizeof(int));

return A;}

Page 103: Dynamic Memory Allocation

103

10.11.6 Referencing a Pointer Instead of the Object it Points to

Page 104: Dynamic Memory Allocation

104

Precedence and associativity of C operators

int *binheapDelete(int **binheap, int *size){

int *packet = binheap[0];

binheap[0] = binheap[*size-1];*size--;heapify(binheap, *size, 0);return(packet);

}

Page 105: Dynamic Memory Allocation

105

10.11.7 Misunderstanding Pointer Arithmetic

Page 106: Dynamic Memory Allocation

106

Common mistake

int *search(int *p, int val){

while (*p && *p != val)p += sizeof(int);

return p;}

• Forget that arithmetic operations on pointers are performed in units that are the size of the objects they point to, which are not necessarily bytes

Page 107: Dynamic Memory Allocation

107

10.11.8 Referencing Nonexisted Variables

Page 108: Dynamic Memory Allocation

108

Discipline

int *stackref(){

int val;

return &val;}

• The stack discipline will sometimes reference local variables that are no longer valid

Page 109: Dynamic Memory Allocation

109

10.11.9 Referencing Data in Free Heap Blocks

Page 110: Dynamic Memory Allocation

110

Error

int *heapref(int n, int m){

int i;int *x, *y;

x = (int *) Malloc(n * sizeof(int));

free(x);

y = (int *) Malloc(m * sizeof(int));for (i=0; i<m; i++)y[i] = x[i]++;return y;

}

• To reference data in heap blocks that have already been freed

Page 111: Dynamic Memory Allocation

111

10.11.10 Introducing Memory Leaks

Page 112: Dynamic Memory Allocation

112

Error

void leak (int n){

int *x = (int *) Malloc(n * sizeof(int));

return;}

• Create garbage in the heap by forgetting to free all allocated blocks

Page 113: Dynamic Memory Allocation

113

10.12 Recapping Some Key Ideas About Virtual Memory

Page 114: Dynamic Memory Allocation

114

10.13 Summary