Java Reference
In-Depth Information
,andJava TM ,pop-
ularized dynamically allocated data that can be created or released at any
time during execution. Dynamic data require heap allocation , which allows
memory blocks to be allocated and freed at any time and in any order during
program execution. With dynamic allocation, the number and size of data ob-
jects need not be fixed in advance. Each program execution can “customize”
its memory allocation needs.
All memory allocation techniques utilize the notion of a data area .Adata
area is a block of storage known by the compiler to have uniform storage
allocation requirements. That is, all objects in a data area share the same data
allocation policy. The global variables of a program can comprise a data area.
Space for all variables is allocated when execution of a program begins, and
variables remain allocated until execution terminates. Similarly, a block of
data allocated by a call to new or malloc forms a single data area, because the
entire block remains allocated until it is explicitly freed or collected.
We will begin our study of memory allocation with static allocation in
Section 12.1. Stack-based memory allocation is investigated in Section 12.2.
The structure and layout of arrays are considered in Section 12.3 and heap
storage is studied in Section 12.4.
First Lisp and subsequently languages like C, C
++
,C
12.1 Static Allocation
In the earliest programming languages, including all assembly languages,
COBOL, and Fortran, all storage allocation was static. Space for data objects
was allocated at a fixed memory address for the lifetime of a program. Use
of static allocation is feasible only when the number and size of all objects to
be allocated is known at compile-time. Static allocation makes storage allo-
cation almost trivial, but it can also be quite wasteful of space. As a result,
programmers sometimes found it necessary to overlay variables. In Fortran,
for example, the equivalence statement was commonly used to reduce stor-
age needs by forcing two variables to share the same memory locations. (The
C
union construct can do this too.) Overlaying impairs programreadabil-
ity because assignment to one variable implicitly changes the value of another.
As a consequence, it can also lead to subtle programming errors.
Inmoremodern languages, static allocation is used for global variables that
arefixedinsizeandaccessiblethroughout program execution. It is also used
for program literals (constants) that need to be fixed throughout execution.
Static allocation is used for static and extern variables in C
/
C
++
/
C
++
and for
static fields in C
and Java classes. Static allocation is also routinely used for
program code, since fixed runtime addresses are required in branch and call
instructions. Also, since control flow within a program is very hard to predict,
it is di
cult to know which instructions will be needed next. Accordingly, if
 
 
Search WWH ::




Custom Search