pnh.stg.mgmt

pnh.stg.mgmt - Chapter 10 Storage Management [These notes...

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

View Full Document Right Arrow Icon

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

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

Unformatted text preview: Chapter 10 Storage Management [These notes are slightly modified from notes on C storage allocation from the Fall 1991 offering of CS60C. The language used is C, not Java.] 10.1 Classification of storage In languages like C or Java, the storage used by a program generally comes in three categories. Static storage. This refers to variablesgenerally given names by declarations whose lifetime by definition encompasses the entire programs execution. Local storage. Variablesalso usually named in declarationswhose lifetimes end after the execution of some function or block. Dynamic storage. Variables (generally anonymous) whose lifetime begins with the evaluation of a specific statement or expression and ends either at an explicit deallocation statement or at program termination. For example, in Java, static variables are introduced by as static fields in classes. C and C++ also allow for static variables in functions and outside classes and functions (at the outer level where they are in effect static fields in a giant anonymous class). For example, int rand(void) /* C code */ { static int lastValue = 42; extern int randomStatistics; ... } Here, there is a single variable lastValue and a single variable randomStatistics that retain their last values from call to call. It is true that only the function rand 143 144 CHAPTER 10. STORAGE MANAGEMENT is allowed to access lastValue by name, but that is an independent question 1 . Local variables in Java and C are simply non-static, non-external variables or parameters declared in a function. They disappear upon exit from the function, which is why the following piece of code, beloved of C beginners, is almost certainly incorrect. int* newIntPointer(int N) /* C code */ /* Return a pointer to an integer initially containing N. */ { int X = N; return &X; } In C, one can have pointers to simple containers: &X creates a pointer to the con- tainer X , and int* denotes the type pointer-to- int . The variable X ocially dis- appears immediately after the return. Practically speaking, this means that the compiler is allowed to re-use the storage location that was used to contain X at any subsequent time (which will probably be the very next call to any function). Finally, dynamic variables in Java and C++ are the anonymous objects the programmer creates using new , or in C using calloc or malloc . In C and C++, any deallocation that takes place must be explicit (by use of the free function or delete operator, respectively). Languages like Java and Lisp have no explicit free operation, and instead deallocate storage at some point where the storage is no longer needed. Well discuss how later in this chapter Just to show that hybridization is possible, some C implementations support a function called alloca . This takes the same argument as malloc and returns a pointer to storage. But the lifetime of the storage ends when the function that called alloca exits (one may not free storage allocated by...
View Full Document

Page1 / 23

pnh.stg.mgmt - Chapter 10 Storage Management [These notes...

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

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