Data Structures and Algorithms Arrays: Amjad Ali
Data Structures and Algorithms Arrays: Amjad Ali
Amjad Ali
Introducation
A collection of pairs <index,value> where index is an ordered set of integers and are values of some data type that is constant for the array. not all languages require index to be continuous or contiguous or start at 0 or 1. In C arrays are zero based and are contiguous from 0 to size-1 and can contain any simple or aggregate data type
cont..
Pascal allows discontinuous indices
A(2:5, 10:20, 26) other index values are undefined and take up no memory
Perl allows indices that are not integers but are literal values (called an associative array)
A[tom] , A[dick], A[harry]
Array Types
Static arrays arrays allocated at compile time Dynamic arrays arrays allocated by the storage management system at program run time
Array Operations
Basic operations: create(A) allocates storage retrieve(A,i) return v at position i in A store(A,I,v) store v at position i in A destroy(A) deallocate storage associated with A
allocated as part of the program space by the compiler. &a is equivalent to a and is the address of a[0] once allocated cannot be deallocated, will always take up program space
cmalloc works same way but initializes array to 0 initialization to anything else requires a loop realloc will resize a previously allocated array to bigger or smaller since this happens at run time, time is expended
store
done the same way for both static and dynamic arrays by using the assignment operator (=) a[5] = 9;
retrieve
retrieving a value from some position in an array is done the same way for both static and dynamic arrays using the array position implicitly. x[3] ; //the value of the 4th element of x can be used this way in any assignment, arithmetic/logical operation or as an argument in a function call
destruction of a statically allocated array happened when the program is done destruction of dynamically allocated arrays is done using the free(arrayname)
destroy
delete [ ] d;
The brackets tell C++ a dynamic array is being deleted so it must check the size to know how many indexed variables to remove
this returns the storage to the storage management system for subsequent allocation for something else. forgetting to deallocate unneeded storage is called a memory leak and can cause a program terminate abnormally (crash)
memory
remember, a computers memory is really an array of bytes (indicies 0 to size-1) every time an array access (retrieve or store) is done the machine must make a calculation to see where in memory the desired location is: ex int a[5]; a[3]=2; to calculate the address of a[3] address=base address+(index*element size) = 100016 + (316*416) = 100c16
base address is assigned by compiler for static and by SMS for dynamic and kept track of in a system table for run time
cont.
int a[2][2]
0123
0x100000
0x100004 0x100008 0x10000C
0,0
0,1 1,0 1,1 a[m][n] ) This storage arrangement is known as: Row Major Order
SMF = base addr + (dim(n) * element size * indexm ) + (element size * indexn )