17 EECS 280 Memory Models

qiuhuaiyao

贡献于2014-10-27

字数:0 关键词: 嵌入式开发

EECS 280 Programming and Introductory Data Structures Memory Models Slides by Andrew DeOrio, Jeff Ringenberg and Brian Noble Static, fixed-size structures So far, the data structures we've built have all had room for "at most N" elements: The various IntSet implementations could have at most 100 distinct integers We could extend these sizes to larger ones, but we really only know how to create static, fixed-sized structures. 2 Static, fixed-size structures Sometimes, the process you are modeling has a physical limit, which makes a static, fixed-sized structure a reasonable choice. For example, a deck of cards has 52 individual cards in it, so this is a reasonable limitation. However, there is no meaningful sense in which a "set of integers" is limited to some particular number of elements. No matter how big you make the set's capacity, an application that needs more will eventually come along. Consider the list_t type from the second project: The type imposed no limits on how large a list could grow 3 Global and local variables For the variables we have used so far you need to know two things at compile time (statically) The size (or number) The lifetime (when it will be created and destroyed) There have been two classes of such variables: 1. Global variables 2. Local variables 4 Global variables Global variables are defined anywhere outside of a function definition Space is set aside for these variables before the program begins execution, and is reserved for them until the program completes This space is reserved at compile time 5 Global variable examples 6 const int SIZE=10; int main() { //... } static Triangle g_triangle; Shape * ask_user() { //... return &g_triangle; } int sum_cur=0; //bad idea int sum(list_t list){ // ... } int prod_cur=0;//bad idea int product(list_t list){ // ... } Local variables Local variables are any variable defined within a block This includes function arguments, which act as if they were defined in the outermost block of the function Space is set aside for these variables when the relevant block is entered, and is reserved for them until the block is exited This space is reserved at run time, but the size is known to the compiler Since the compiler must know how big all of these variables will be, it is static information, and must be declared by the programmer 7 Local variable examples int sum(list_t list){ //... } int sum(int *array, int size) { int sum=0; //... } int main(int argc, char* argv[]) { IntSet i; for (int i=0; i<10; ++i) { //... } } 8 Dynamic variables There is a third type of variable, called a dynamic variable It is dynamic because Size (or number) is determined at runtime When it will be created and destroyed is determined at runtime In other words, you (the programmer) get to decide how big a dynamic variable is, when it is created, and when it is destroyed 9 new Create dynamic variables using new int main() { int *p = new int; } This creates new space for an integer, and returns a pointer to that space, assigning it to p The initial value is undefined Use initializer syntax to assign an initial value int main() { int *p = new int(5); } 10 Exercise int main() { int *p = new int(5); } How many variables are in this program? Mark each as global, local or dynamic 11 Exercise int main() { int *p = new int(5); } How many variables are in this program? 2 1. int *p is a local variable of type "pointer to integer" 2. int(5) is a dynamic variable of type "integer" This “thing pointed to by p” is a dynamically-allocated piece of memory, and lives “somewhere else”. It does not have a name of its own, but is “the integer pointed  to by p”. 12 p: 5 delete Destroy dynamic variables using delete int *p = new int; //do something with p delete p; Releases the claim on the space previously used by the int Space can be recycled later by new 13 delete pitfalls You can only delete a dynamic variable once int *p = new int; //do something with p delete p; delete p; //Error! 14 *** glibc detected *** ./a.out: double free or corruption (fasttop): 0x00000000007ac010 *** ... Aborted (core dumped) delete pitfalls delete 0 (AKA NULL) does nothing int *p = new int; //do something with p delete p; p=0; delete p; //OK 15 delete pitfalls Ordinary objects can be destroyed by delete, but only if they were created by new int i = 0; //local variable int *p=&i; //pointer to local variable delete p; //Error! 16 Size of dynamic variables With dynamic arrays, we can choose the size at runtime //ask user to enter integer size int size = get_size_from_user(); int *p = new int[size]; //do something with p ... delete[] p; Note the different syntax with delete We will talk about dynamic arrays in more depth next time 17 Lifetime of dynamic variables What’s the problem? //EFFECTS: allocates an array of specified size // and initializes each element to zero int * get_zero_array(int size) { int array[size]; for (int i=0; i int *ptr=NULL; Works, but ptr=0 is preferred 42 Stack and heap exercise int i = 42; int *p = &i; delete p; p=0; Draw the stack and the heap What is wrong with this code? 43 stack heap Stack and heap exercise int i=4; int *p = new int(17); i = *p; delete p; p=0; Draw the stack and the heap How much memory is leaked? 45 stack heap Stack and heap exercise int *p = new int(100); int *q = p; delete q; q=0; cout << *p << endl; Draw the stack and the heap What does this print? How much memory is leaked? 48 stack heap Stack and heap exercise int *p = new int(100); int *q = new int(42); q=p; delete q; q=0: Draw the stack and the heap How much memory is leaked? 51 stack heap Classes and dynamic memory When you create instances of classes, their constructors are called, just as if it were created the "normal" way. IntSet *isp = new IntSet; 1. Allocate enough space on the heap to hold an IntSet. An array of 100 integers (elts) One extra integer (elts_size) to hold its size 2. Call the constructor IntSet::IntSet() on this new object 55 Classes and dynamic memory We can also destroy instances of ADTs that were created by new: IntSet *isp = new IntSet; delete isp; isp=0; 56 Exercise: allocating classes IntSet *isp = new IntSet; delete isp; isp=0; Draw the stack and heap Assume no virtual functions 57 stack heap Exercise: allocating classes IntSet *isp = new IntSet; delete isp; isp=0; Draw the stack and heap Assume no virtual functions 57 stack heap

下载文档,方便阅读与编辑

文档的实际排版效果,会与网站的显示效果略有不同!!

需要 3 金币 [ 分享文档获得金币 ] 0 人已下载

下载文档

相关文档