CIS3355:
Business Data Structures |
What are offsets and how do they work?
To begin defining an offset, we must first know what an array is. An array is a data structure that allows storage of a sequence of values. The values are stored in a contiguous block of memory. Arrays allow fast random access to particular elements. In other words, arrays are collections of a fixed number of objects of all the same type (int, char, etc.) which are stored sequentially in computer memory. For demonstration purposes, an array which contains 5 integer values of type int called example could be represented this way:
example
In this particular array, the blank panels represent an element of the array (the array’s objects). These particular objects or elements, are the type int. Take note that they are also in sequential order. By being in sequential order, if the computer knows where the first element is, then it is relatively easy for it to find the second, the third, and so forth. Also since this is an array of type int, we are asking for 2-bytes per element (2*4=8 contiguous bytes of RAM) Now that we what an array is we can move onto what offsets (a.k.a. displacement, subscript, or index) are and how they work! If you noticed carefully, the array did not begin with the number 1. Rather it began with the number 0. Each element is offset from the beginning of an array starting at 0. Why is the first element labeled 0? Well its because it is zero elements away from the beginning of the array. This means that the second element is actually 1 away from the beginning the array. The third element is 2 away from the beginning of the array and the forth, well you get the picture! Any array of size N has an offset range of 0 to N-1. In our case the range is from 0 to 4. Now to the tricky part! Let’s take an array and figure out the offset by using the formula : ELEMENT ADDRESS = BASE ADDRESS OF THE ARRAY+ (OFFSET # * 2) *Remember we have to multiply the offset by 2 since we are using integers. Therefore the 2 bytes * 4 elements = 8 continguous bytes of RAM needed! First let’s say our base address starts at 100.
int example[4] ={1,3,5,7} Element number/position/index Addresses Reference 1 100 and 101 example[0] 2 100+2=102 (& 103) example[1] 3 102+2=104 (& 105) example[2] 4 104+2=106 (&107) example[3]
RAM Allocation:
Offset Address: 0 100 +( 0*2) =100 1 100 +(1*2) =102 2 100 +(2*2) =104 3 100 +(3*2) =106
We would follow the same procedure for a character array and float array except for a minor change. We need to multiply the offset by 1 and then add it to the base address for a character type. For the float, we multiply the offset by 4 since we need 4 bytes of storage then add it to the base address. References: C++ Tutorial: 3.1, Arras C Tutorial - Lesson 9: Arrays
Question:
1. If my base address were 23456 and int example[4] ={0,4,8,12} What would my address be for the third offset? Answer:
Offset Address: 0 23456 + (0*2) =23456 1 23456 +(1*2) =23458 2 23456 + (2*2) = 23460 3 23456 + (3*2) = 23462
My third offset is actually offset number 2, not 3. Offsets always start at 0 not at Therefore my address for the third offset will be 23460.
Question 2. What would happen if I had a float instead of an integer in the previous problem? Explain.
Answer: Instead of multiplying the offset number by 2, I would have to multiply it by 4 to get the address of the third offset.
Offset Address: 0 23456 + (0*4) =23456 1 23456 + (1*4) =23460 2 23456 + (2* 4) =23464 3 23456 + (3*4) =23468
My answer would then be 23464 because offset 2 is my third offset.
3. Question: Which of the following is true about offsets?
Answer: B is true.
|