Dynamic Memory Allocation with the Heap - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

Dynamic Memory Allocation with the Heap

Description:

Dynamic Memory Allocation with the Heap – PowerPoint PPT presentation

Number of Views:31
Avg rating:3.0/5.0
Slides: 19
Provided by: usersEc3
Category:

less

Transcript and Presenter's Notes

Title: Dynamic Memory Allocation with the Heap


1
Dynamic Memory Allocation with the Heap
2
Three Flavors of Memory
  • C programs consist of three types of variables
  • local variables
  • global variables
  • dynamically allocated variables (weve not seen
    these yet).
  • Each of type of variable managed differently.

3
Global Variables
  • Global variables in C are allocated using the
    traditional memory map approach.
  • After the compiler counts the amount of memory
    required for the program, it starts counting the
    memory required for globals.
  • By convention, the block of memory that holds the
    program is called the text segment (or
    sometimes, the code segment)
  • Global variables are stored in the data segment

4
Global Variable Lifetimes
  • Global variables are created when the program
    first starts running.
  • initialization of these variables occurs before
    main begins running!
  • if you forget to initialize your global variable,
    then it is automatically initialized to zero.
  • Global variables are destroyed when main returns.

5
Local Variable Lifetimes
  • Local variables (including parameters) are
    created when a function is called and go away
    when that function returns.
  • Of course, the memory location is still there,
    its just that the variables name no longer can
    be used to access that memory location.
  • Functions can call other functions
  • The order for allocation/deallocation is Last in
    First Out. Hence, a stack is used.

6
Dynamic Allocation
  • Sometimes we have a need in our program to store
    an unpredictably large amount of data.
  • For example, a word processor has no idea how
    long your document will be until you actually
    type the whole thing in.
  • Ideally, wed like to make variables (or arrays
    of variables) on the fly.
  • For example, each iteration of a while loop we
    might want to create a new variable.

7
An example application
  • Remember Project 3 the word counting program?
    What if we wanted to count the number of unique
    words.
  • To implement this new algorithm wed need to
    remember which words wed already seen.
  • To store a word in memory, we need a whole array
    of characters.
  • But before reading the file, how do we know how
    many arrays well need, or even, how many letters
    in each array?

8
Simple Allocation
  • As we saw in project 4, memory allocation does
    not need to be hard.
  • The simplest case is when the program never
    forgets.
  • Once the program allocates memory to hold some
    dynamic information, the program will always need
    to keep that memory around.
  • In this case, we can implement our allocator by
    slicing chunks of memory from an array.
  • Start at the bottom and work our way towards the
    top.

9
A Whole Heap of Memory
  • Lets assume that we have a large array that
    well call the heap.
  • As the program runs, well use parts of this
    array to store the information we need.
  • In the real world the heap is indeed a big
    array, but it is allocated to the program by the
    operating system.
  • Two subroutines are used to access this array
  • malloc is a routine that returns the address of
    an unused section of the heap.
  • free is a routine that we can use (or not) when
    we have finished with a part of the array.

10
Slicing Chunks
  • The operating system always locates the heap at a
    position in the address space where theres lots
    of room to grow.
  • The OS remembers where the heap ends.
  • If the program wants more heap, then it asks the
    OS to move the end of the heap a little further
    up in the address space.
  • The malloc function slices a chunk off this array
    and returns the address of the first byte in it.
  • The free routine makes an annotation that
    indicates that this chunk is available for future
    use.

11
Playing Nicely with The Heap
  • When you are done using the memory, its
    considered polite to free it. You free the
    memory by calling free with a pointer to the
    memory.
  • int p (int) malloc(sizeof(int))
  • free(p)
  • int my_array (int) malloc(10 sizeof(int))
  • free(my_array)

12
But, How does it Work?
  • There are gazillions of different memory
    allocation strategies. Donald Knuth is credited
    with one of the most straightforward,
    general-purpose algorithms.
  • First, well make a few preliminaries.
  • assume heap is an array (of int) we can use.
  • a chunk will consist of one or more elements of
    the heap array.
  • initially, the heap has just one junk (a really
    big one).

13
Chunks and Signatures
  • Each chunk begins with signature.
  • the signature is an int (obviously)
  • positive indicates the chunk is available
  • negative indicates the chunk is in use.
  • the magnitude of the signature indicates the size
    of the chunk.
  • Each chunk ends with a copy of its signature.
  • Whats the smallest possible chunk?
  • Three elements (two signatures and at least 1
    element to hold the data).

14
Allocation Give the Customer more than they ask
for
  • When someone calls malloc we must identify a
    chunk thats at least as large as the amount
    requested. Using an extra-big chunk is OK.
  • The user is supposed to only use the amount of
    space they request.
  • They really shouldnt ever notice whether we gave
    them exactly that amount, or if we gave them more.

15
Splitting Chunks
  • We will try to keep as many big chunks as we can
    (well, while still keeping things simple).
    Frequently, when someone allocates memory, well
    find a big chunk and carve a small piece of for
    them.
  • Note that we wont split a chunk unless both of
    the pieces are bigger than some minimum size.
    The minimum size is typically 1 word of useful
    space plus two words to hold the signatures
    (total of 3).

16
Deallocating Memory
  • When our user is done with the memory they asked
    for, they should return the memory to us by
    calling delete.
  • Our first task is identifying which chunk theyre
    returning.
  • We can find the signature by subtracting 1 from
    the address they were using!
  • This is because the space inside a chunk begins
    immediately after the signature, i.e., the
    signature can be found immediately before the
    first address of the memory the user was using.

17
Marking and Merging Chunks
  • Once weve identified the chunk and have found
    the signature, we change the sign (making it
    positive) of both signatures.
  • Our last step is to combine any adjacent,
    available, small chunks into an available big
    chunk.
  • In the worst case, there is one available chunk
    immediately before this chunk, and also one
    available chunk immediately after this chunk.
  • why cant there be more? cause we always combine
    chunks if its possible, so if there were two
    chunks in a row that are available, wed have
    already combined them!

18
How to Merge
  • To merge a chunk with its successor, simply
    recalculate what the combined size is (its the
    sum of the sizes of the two chunks plus two).
  • Write this new size into the bottom signature on
    the bottom chunk and replace the top signature on
    the top chunk with the same size.
  • We dont need to erase the old signatures (which
    are now in the middle of the chunk). Since the
    signatures are not at the beginning or end of any
    of our chunks, no one will ever see them there!
  • I suppose, if you want to be neat, you can reset
    these signatures to zero, no harm done.
Write a Comment
User Comments (0)
About PowerShow.com