This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Subscript Bindings and Array Categories• In some languages the lower bound of the
subscript range is implicit, e.g. C-based languages – 0; Fortran 95 – 1. In some other languages, subscript ranges must be completely specified by the programmer.
Subscript Bindings and Array Categories• Five categories of arrays
– Static Array: subscript ranges are statically bound; storage allocation is static (done before run-time). Advantage: efficiency, no dynamic allocation or deallocation is required. E.g.: C. C++ static variables
– Fixed stack-dynamic array: subscript ranges are statically bound, but the allocation is done at declaration elaboration time during execution. Advantage: space efficiency, share space among different subprograms. E.g.: arrays declared in C, C++ functions
Subscript Bindings and Array Categories– Stack-dynamic array: subscript ranges are dynamically bound, and storage allocation is done during run time. Once the ranges are bound and the storage allocated, they remain fixed during the lifetime of the variable. Advantage: flexibility, the size of an array need not be known until the array is about to be used. E.g.: Ada arrays
– Fixed heap-dynamic array: subscript ranges and storage are dynamically bound during run time when the user program request them. Fixed when they are allocated. The storage is allocated from the heap. E.g.:C, C++, Java, C#
Subscript Bindings and Array Categories– Heap-dynamic array: the binding of subscript ranges and the storage allocation is dynamic and can change any number of times during the array’s lifetime. Advantage: flexibility. E.g.: C# ArrayList, Perl, JavaScript
• Some language allow initialization at the time of storage allocation– C, C++, Java, C# exampleint list [] = {4, 5, 7, 83} – Character strings in C and C++char name [] = “freddie”;– Arrays of strings in C and C++char *names [] = {“Bob”, “Jake”, “Joe”];– Java initialization of String objectsString[] names = {“Bob”, “Jake”, “Joe”};
• A rectangular array is a multi-dimensioned array in which all of the rows have the same number of elements and all columns have the same number of elements– Supported by Fortran, Ada and C#.– myArray[3,7]
• A jagged matrix has rows with varying number of elements– Possible when multi-dimensioned arrays actually appear
as arrays of arrays– Supported by C, C++ and Java– myArray[3][7]
• An associative array is an unordered collection of data elements that are indexed by an equal number of values called keys – User defined keys must be stored– A key value pair– Access the value through the key
• Design issues: What is the form of references to elements?
e.g. Emp_Rec.Name• Fully qualified references must include all record names,
from the largest enclosing record to the specific field• Elliptical references allow leaving out record names as long
as the reference is unambiguous, for example in COBOLFIRST, FIRST OF EMP-NAME, and FIRST of EMP-REC are elliptical references to the employee’s first name
• Fortran, C, and C++ provide union constructs in which there is no language support for type checking; the union in these languages is called free union
• Type checking of unions require that each union include a type indicator called a discriminant– The Union is called a discriminated union– Supported by Ada
• Two fundamental operations: assignment and dereferencing
• Assignment is used to set a pointer variable’s value to some useful address
• Dereferencing yields the value stored at the location represented by the pointer’s value– Dereferencing can be explicit or implicit– C++ uses an explicit operation via *j = *ptrsets j to the value located at ptr
Reference Types• C++ includes a special kind of pointer type called a
reference type that is used primarily for formal parameters– Advantages of both pass-by-reference and pass-by-value
#include <iostream> using namespace std; void f( int ); int main() { int x = 5; cout << "Before f(x): " << x << endl; f( x ); cout << "After f(x): " << x << endl; return 0;
} void f( int n ) { cout << "1. In f(x): " << n << endl; n = 32;
cout << "2. In f(x): " << n << endl;
}
#include <iostream> using namespace std; void f( int & ); int main() { int x = 5; cout << "Before f(x): " << x << endl; f( x ); cout << "After f(x): " << x << endl; return 0; } void f( int & n ){ cout << "1. In f(x): " << n << endl; n = 32; cout << "2. In f(x): " << n << endl;
Approach one: Reference Counter• Reference counters: maintain a counter in
every cell that store the number of pointers currently pointing at the cell. When counter reaches 0, time for garbage collection.– Disadvantages: space required, execution
time required, complications for cells connected circularly322