PPL-Unit 2 Part 4
PPL-Unit 2 Part 4
Design Issues
The primary design issues specific to arrays are the
following:
• What types are legal for subscripts?
• Are subscripting expressions in element references
range checked?
• When are subscript ranges bound?
• When does array allocation take place?
• Are jagged or rectangular multidimensioned arrays
allowed, or both?
Array
Arrays and Indices
•Specific elements of an array are referenced by means
of a two-level syntactic mechanism, where the first part
is the aggregate name, and the second part is a
possibly dynamic selector consisting of one or more
items known as subscripts or indices.
Syntactic representation
array_name(subscript_value_list)
Example in c
Int a[10];
For example, in Ada
type Week_Day_Type is (Monday, Tuesday, Wednesday,
Thursday, Friday);
type Sales is array (Week_Day_Type) of Float;
Array
Subscript Bindings and Array Categories
•The binding of the subscript type to an array variable
is usually static, but the subscript value ranges are
sometimes dynamically bound.
•There are five categories of arrays, based on the
binding to subscript ranges, the binding to storage, and
from where the storage is allocated.
Static array
•A static array is one in which the subscript ranges are
statically bound and storage allocation is static.
•Arrays declared in C and C++ functions that include
the static modifier are static.
Advantage
•Static arrays are efficienct: No dynamic allocation or
deallocation is required.
Array
Disadvantage
•In this static arrays the storage for the array is fixed
for the entire execution time of the program.
fixed stack-dynamic array
•A fixed stack-dynamic array is one in which the
subscript ranges are statically bound, but the allocation
is done at declaration elaboration time during
execution.
•Arrays that are declared in C and C++ functions
(without the static
specifier) are examples of fixed stack-dynamic arrays.
Advantage
•fixed stack-dynamic arrays over static arrays is space
efficiency.
Disadvantage
•fixed stack-dynamic arrays requires allocation and
Array
stack-dynamic array
•A stack-dynamic array is one in which both the
subscript ranges and the storage allocation are
dynamically bound at elaboration time.
•Ada arrays can be stack dynamic
Advantage
•stack-dynamic arrays over static and fixed stack-
dynamic arrays is flexibility.
heap-dynamic array
•A heap-dynamic array is one in which the binding of
subscript ranges and storage allocation is dynamic and
can change any number of times during the array’s
Array
Advantage
•The advantage of heap-dynamic arrays over the others is
flexibility:
Arrays can grow and shrink during program execution as
the need for space changes.
Disadvantage
•The disadvantage is that allocation and deallocation take
longer
and may happen many times during execution of the
program
Array Initialization: