170 likes | 187 Views
This chapter explores the implementation of arrays in memory, including 1D and 2D arrays. It discusses row-major and column-major order, address calculation, and provides examples.
E N D
Chapter 6 Part 1
Array Implementation 1 2 k-1 k n 0 M … … • 1D Array Memory Base Address Address(list[k]) = address(list[1]) + (k-1) * element_size Generalization: Address(list[k]) = address(list[lower_bound]) + (k - lower_bound) * element_size • Example: Array A with following information: • 10 elements • Element size = 10 bytes • Base address = 1500 • Lower bound = 0 • Address( A[5]) = ? • Address( A[5]) =1500 + 5*10 = 1550
2D Arrays 2 common ways for mapping 2D arrays to 1D: 1) Row Major Order 2) Column Major Order Ex: 3 4 7 6 2 5 1 3 8 Would be stored as 3, 6, 1, 4, 2, 3, 7, 5, 8 Ex: 3 4 7 6 2 5 1 3 8 Would be stored as 3, 4, 7, 6, 2, 5, 1, 3, 8
In general: address of an element = base address + element size * number of elements that precede it. 2D Array - Row Major Order 1 2 … j-1 j … n 1 2 ... i-1 i ... m
In general: address of an element = base address + element size * number of elements that precede it. 2D Array - Row Major Order 1 2 … j-1 j … n 1 2 ... i-1 i ... m
In general: address of an element = base address + element size * number of elements that precede it. 2D Array - Row Major Order 1 2 … j-1 j … n 1 2 ... i-1 i ... m Address(a[i,j]) = address(a[1,1]) + ((((number of rows above i)* (row size)) + (number of elements left j)) * element size) = address(a[1,1]) + ((((i-1)* n) + (j-1)) * element_size) Generalization: Address(a[i,j]) = address(a[row_lb,col_lb]) + ((((i-row_lb)* n) + (j-col_lb)) * element_size)
In general: address of an element = base address + element size * number of elements that precede it. 2D Array - Row Major Order 1 2 … j-1 j … n 1 2 ... i-1 i ... m • Example: Array A with following information: • 15 rows and 5 columns • Element size = 10 bytes • Base address = 1000 • Row lower bound =column lower bound = 0 • Address( A[2,3]) = ? • Address( A[2,3]) =1000 + (((2-0)*5) + (3-0)) *10 = 1130
In general: address of an element = base address + element size * number of elements that precede it. 2D Array - Column Major Order 1 2 … j-1 j … n 1 2 ... i-1 i ... m
In general: address of an element = base address + element size * number of elements that precede it. 2D Array - Column Major Order 1 2 … j-1 j … n 1 2 ... i-1 i ... m
In general: address of an element = base address + element size * number of elements that precede it. 2D Array - Column Major Order 1 2 … j-1 j … n 1 2 ... i-1 i ... m Address(a[i,j]) = address(a[1,1]) + ((((number of columns left j)* (col size)) + (number of elements above i)) * element size) = address(a[1,1]) + ((((j-1)* m) + (i-1)) * element_size) Generalization: Address(a[i,j]) = address(a[row_lb,col_lb]) + ((((j-col_lb)* m) + (i-row_lb)) * element_size)
In general: address of an element = base address + element size * number of elements that precede it. 2D Array - Column Major Order 1 2 … j-1 j … n 1 2 ... i-1 i ... m • Example: Array A with following information: • 15 rows and 5 columns • Element size = 10 bytes • Base address = 1000 • Row lower bound =column lower bound = 0 • Address( A[2,3]) = ? • Address( A[2,3]) =1000 + (((3-0)*15) + (2-0)) *10 = 1470
Records Compile-time descriptor Field 1 Address relative to the beginning of the record Field n
Compile-time descriptor Field 1 Field 2 Address (Name) = base address + 0 Address (Salary) = base address + 20 Address (Year) = base address + 28 Field 3 Base Address Name Salary Year 0 M 20 bytes 8 bytes 4 bytes 0 20 28 Memory Relative address = offset
Union • A union is a type that may store different type values at different times during execution.
Common part Variant part Variant Records • A single record type with several variants. • Specification: • In Pascal: type PayType=(Salaried, Hourly); varEmployee:record ID : integer; Dept: array[1..3] of char; Age : integer; casePayClass : PayTypeof Salaried:(MonthlyRate:real; StartDate :integer); Hourly :(HourRate:real; Reg :integer; Overtime:integer) end
Common part Variant part • In C: (use the ‘union’ keyword) typedefenum{Salaried,Hourly} PayType; typedefstruct{ int ID; char Dept[3]; int Age; PayTypePayClass; PayVariant pay; } EmployeeRecord; EmployeeRecord Employee; typedefstruct{ float MonthlyRate; intStartDate; } SalariedType ; typedefstruct{ float HourRate; intReg, Overtime; } HourlyType ; typedefunion{ SalariedTypesal_type; HourlyTypehour_type; } PayVariant ;