160 likes | 314 Views
Elementary Data structures. Arrays. Sequence of elements of similar type. Elements are selected by integer subscripts. For example, signs: array [37:239] of integer. records. Sequence of elements having diverse types and sizes.
E N D
Arrays • Sequence of elements of similar type. • Elements are selected by integer subscripts. • For example, • signs: array[37:239] of integer.
records • Sequence of elements having diverse types and sizes. • Elements of a record are called fields. directory_entry directory_entry: record type name: string length = 20 address: string length = 20 phone_number: integer callee: directory_entry phone_number(callee)← 751489 or phone_number[callee]← 751489 callee
Records within records • address_type: record type street_number:integer street_name:string length=16 • directory_entry: record type name: string length = 20 address: address_type phone_number: integer • customer:directory_entry • Now we can refer to an entire address as in: • address(customer) • street_name(address(customer) • address(payer) ← address(customer)
Records within records • There can be record types in which one or more of the fields are themselves records of the same type. • directory_entry: record type name: string length = 20 address: address_type phone_number: integer subsidiary_listing:directory_entry • listing : directory_entry • A variable declared like this appears to require infinite storage as shown below:
Storage layout for a directory_entry requiring infinite storage
Records and pointers • To avoid the need for infinite storage, we can use pointer variables rather than record variables. • A pointer variable does not itself contain the fields of a record ; instead it specifies the location of the actual record value. • Thus, declarations of the following form : variable_name:record_type • will cause space to be allotted only for a pointer, not for a record. • To create the record values themselves we will use the primitive getcellsuch as • listing ← getcell, • address(listing) ←getcell • subsdiary_listing (listing)←getcell
Example of record variables implemented as pointers to records listing directory_entry address directory_entry ? directory_entry ? ?
IMPLEMENTATIONS IN MEMORY The memory of a computer composed of a sequence of words. Each word is referred to by its unique address. The address of the first word is normally 0 A memory of 2k words to have addresses that are k bits long. We use the notation: loc(variable), to represent the address to be used for the variable. loc(x), returns the address of x in memory.
IMPLEMENTATIONS IN MEMORY(ARRAYS) Arrays are arranged in order in adjacent memory locations. A : array [l : u] of item (where item is w words long). A[l] is stored, beginning in location L. A[l+1] is stored in location L+w. A[l] is stored in location L+2w. A total of (u – l + 1)w words of memory are used.
In this implementation there is a simple relationship between an index i and loc(A[ i ]) , l ≤ i ≤ u:loc(A[ i ])=loc(A[ l ])+(i-l)w Diagram of an array A[l:u] as arranged in memory
IMPLEMENTATIONS IN MEMORY(TWO DIMENSIONAL ARRAYS) To implement a n x m array, Consider it as an array B[1], B[2],….,B[n] in which B[i] in turn an array of m elements consisting of the i th row of the matrix.
Thus the number of words needed for an element B[i] = mw. Since, w = number of words needed for each element A[i,j]. Thus location of B[i]: loc(B[i]) = loc(B[1])+(i - 1)mw loc(A[i,j]) = loc(B[i]) + (j - 1)w = loc(B[1]) + (i -1)mw + (j - 1)w = loc(B[1]) + [(i-1)m +(j - 1)]w = loc(A[1,1]) + [(i - 1)m+(j - 1)]w
IMPLEMENTATIONS IN MEMORY(RECORDS) A pointer value can be implemented simply as the address of the corresponding record. A record is just a consecutive block of words. Suppose we declare a record type as, type_name: record type field_name1 : item1 field_name2 : item2 ……………………………… field_namen : itemn
P: loc(P)