TYPE Matrix_4x4_type
IS
ARRAY(1..4, 10..13) OF Integer;
If the arrays are allocated during compile time then the range is
statically bound.
- Global or automatic array (value model) variable in C:
int arr [ 40 ];
Automatic Arrays (variable length arrays)
declared in C functions:
void cFunction (int size ) {
int
marbles [size][size/2];
}
Range bound dynamic, storage dynamic
Prior to C99 this code would not compile.
Prior to C99: When multidimensional arrays are used, it is
necessary to specify the bounds of but the first dimension.
When and where does array allocation take
place?
If the arrays are allocated from the heap then both the storage
allocation and the subscript ranges can be dynamically bound.
Java: reference model
- Storage only on the heap
- Arrays are objects
String [ ] sArray = new String[5];
Object [ ] myArray = new Object [sArray.length];
- Java does do range checking. All arrays have a field length
that never changes.
- C, C++ can also dynamically allocate storage from the heap.
- C, C++ does not do range checking.
- Perl:
- variables names for arrays begin with "@"
- storage is dynamically allocated
- example:
@myPerlarr = ("one","two","three");
$myPerlarr[0] is the scalar with value "one"
@myPerlarr[26] = "wow";
- Extends the array to length 26.
How is the array stored in memory?
Runtime calculation of array element.
Computing effective address:
- Arrays in most language implementation are stored in
contiguous location in memory.
- Some languages arrays of arrays.
- Left declaration is a true two-dimensional array.
- Right declarations is an array of pointers to array of chars.
- What are the trade offs?
The key feature of arrays is O (1) access time to any
array element.
Formula for computing the effective address of a one dimensional
array:
- A:
array [LowerBound .. UpperBound]
- Let ba be the base address of array A, and
- Let size be the number of addressable units(bytes) required
to store a value of base type of A.
- Then the effective address of A[k] is:
ba + size * (k - LowerBound of A)
As you can see the cost of computing A[k] is
independent of k.
Multidimensional arrays:
Where does the second element of the array go?
A: array [LowerBound .. UpperBound ] of array [1..4] of char
Row-major -- Rows are contiguous in memory A[2,4] is followed by
A[2,5]
Column-major major columns are contiguous, A[2,4] is followed by
A[3,4]
Fortran uses Column major most languages uses row major --
What is the implications for porting code? (Cache?, program
correctness?)
Can arrays be initialized when they have their storage
allocated?
- Initialization: In FORTRAN, Java, C and Ada, arrays can be
initialized in their declaration.
- Forcing a '2 dimensional' array representation with
initialization in Java:
double[ ] [ ] identity = {
{ 1.0, 0.0, 0.0 },
{ 0.0, 1.0, 0.0 },
{ 0.0, 0.0, 1.0 }
};
- Note: "global" variables in Java are initialized (0,nil)
but NOT local variables.
Haskell, Prolog
- Do not have arrays.
- lists are the way to sequence data.
- How are lists different from arrays?
- Programming in C++, C# and Java you should use collection frameworks
provided in the languages.
Pointers:
- C, C++ pointer types
- the R-value is an address
- int * x;
- float * y;
- C pointers' "units" are adjusted according to the size of the type
they point to.
main(){
char*
ptChr =0;
short*
ptShort =0;
long*
ptLong =0;
int i;
printf("
Index
Char Short Long\n");
for ( i
=0; i < 6; i++) {
printf("Offset
of Pointers %d %d
%2d %2d\n",
i, ptChr+i, ptShort+i, ptLong+i);
}
}
~>
Index Char Short Long
Offset of Pointers 0 0
0 0
Offset of Pointers 1 1
2 4
Offset of Pointers 2
2 4 8
Offset of Pointers 3 3
6 12
Offset of Pointers 4 4
8 16
Offset of Pointers 5 5
10 20
- C++ allows functions to return l-values
int a[10];
int& f(int I) { return (a[I]); }
f(5) = 17;
// This assigns 17 to a[5]
- Pointer (access) types provide a way of manipulating memory
addresses.
- Pointers may be used to create recursive types, e.g. linked list
and trees.
Pointers and Arrays in C
- There is a close correspondence between types "array of Ttype"
and "pointer to Ttype".
- a[i] can be
defined as *(( a )+( i ))
- In fact a[i] is the same as i[a]!
- Since ip = a; is legal is a = ip; legal?
int a[10], *ip;
ip = a;
ip = &a[0];