1 / 17

Array Implementation in Memory

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.

dbutters
Download Presentation

Array Implementation in Memory

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 6 Part 1

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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)

  7. 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

  8. 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

  9. 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

  10. 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)

  11. 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

  12. Records Compile-time descriptor Field 1 Address relative to the beginning of the record Field n

  13. 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

  14. Union • A union is a type that may store different type values at different times during execution.

  15. 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

  16. 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 ;

  17. Implementation:

More Related