The Heap - Dynamic Memory Allocation with the Heap Three...

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon
Dynamic Memory Allocation with the Heap
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Three Flavors of Memory C++ programs consist of three types of variables: local variables global variables dynamically allocated variables (we’ve not seen these yet). Each of type of variable managed differently.
Background image of page 2
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”
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
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.
Background image of page 4
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, it’s just that the variable’s 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.
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
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, we’d 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.
Background image of page 6
A Common Application – Strings Frequently an application will need to store a large number of (relatively small) strings. For example a spell checker must read the entire dictionary. For each string we need an array. But before reading the input file, how do we know how many arrays we’ll need, or even, how many letters in each array?
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Simple Allocation As we saw with multiplying chains of matrices, 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.
Background image of page 8
A Whole Heap of Memory Let’s assume that we have a large array that we’ll call “the heap”. As the program runs, we’ll use parts of this array to
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 07/24/2010 for the course EE 312 taught by Professor Shafer during the Spring '08 term at University of Texas at Austin.

Page1 / 36

The Heap - Dynamic Memory Allocation with the Heap Three...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online