wpe41.gif (23084 bytes)CIS3355: Business Data Structures
Fall, 2008
 

What is an Offsets and how do they work?

 

To understand an offset we first need to understand what an Array is:

An Array is a data structure which  contains a fixed number of contiguous storage elements, all of the same data type.                                         Suppose that we wished to store the first 10 prime numbers (1, 2, 3, 5, 7, 11, 13, 17, 19, 23) in RAM. Of course, we could do this by storing them as individual integer variables (i.e., reserving 10 individual integer addresses) one for each number. *

An numeric Array would allocate the information by having those ten prime numbers sorted in contiguous memory:     

                              int primenumbers[10] = {1, 2, 3, 5, 7, 11, 13, 17, 19, 23}

To better understand an Array the following link is helpful to better understand Numeric Arrays.

 

An Offset is the distance from the first assigned location in RAM of the Array in question. If the offset (A.K.A. Subscript or Index) is  0 (zero) it is referring to the base of the Array. One of the advantages of an array is that we are able to readily find the address of any element in that array using very simple calculations. All we need is two components:
1. The base address of the array (the location in memory where the array begins)
2. The array offset (often also referred to as the subscript or index)*

    To determine the base address (more information in the link) of an Array we can simply request the compiler to gives the location using the command %p (this will gives the address in hexadecimal).

 

To determine an Offset we can:

 

a) Do a table like table 4.1 where we can easily  see the location of the prime numbers and how they would be stored in the locations 1250 to 1269. Although the table only contains 10 integers we used 20 bytes of RAM because one integer requires 2  bytes. By having the table, we can clearly see the location of the prime number 13; which is stored at location 1262 & 1263.

Table 4.1 

1250

1251

1252

1253

1254

1255

1256

1257

1258

1259

1

2

3

5

7

1260

1261

1262

1263

1264

1265

1266

1267

1268

1269

11

13

17

19

23

 

 b) The previous method is the long and complicated way, there is a shorter way to determine an offset which is the formula:

 Element Address = Base Address + Offset**

** (offset number * element)

Using this formula we can determine the Offset of the prime number 13 as follows:

   1250 + 6 * 2 =  1262

1250 is the base address

6 refers to the sixth INT int the array

2 is the number of bytes required by an Integer

We need to remember that the first offset is counted as offset zero so that if we apply the formula we get 1250 + 0 * 2 = 1250 if we count it as one well....                  1250 + 1 * 2 = 1252. Don't forget the first offset is offset 0.

 

An Illegal Offset can occur when we exceed the number of  offsets in our array, for example in the Array int primenumbers[10] = {1...23} represented in table 4.1 we requested 10 locations or 20 bytes of RAM in a contiguous location. To exceed our Array or to get an Illegal offset we would have to reference the 11th location.

int primeumber[9] = 19            this is a legal offset

int primenumber[10] = 23          this is a legal offset

int primenumber[11] = -2442        this is an Illegal offset

   

1266

1267

1268

1269

1270

1271

19

23

11110110

1110110

 As we can see the location 1270 and 1271 have some value which when convert it into decimal we get the value -2442 using the twos compliment. We have to remember that we have in our hands a very fast moron in C++, we asked for the value in location 1270 & 1271 which we didn't assign any value into it so it just read what was there. 

 

* Extract from Abstract Data Structures for Business In C/C++ Kirs and Pflughoeft Chapter 4: Numeric Arrays

 

-----Review-----

------Questions-----

 

Q.1. - What is an Array?

 

 

Q. 2. - What is an offset and what do wee need to determine its position?

 

 

Q. 3. - What is an Illegal offset?

 

 

Q. 4.- How can we determine a Base Address?

    a) It can be assigned with %p
    b) It can be given by %p
    c) It can be given by %lu
    d) answer a & b
    e) answer b & c

 

 Q. 5. - How many different data types we can use in an Array?

    a) 5
    b) 6
    c) 7
    d) 8
    e) 9

Q . 6. - What is the formula for determining an offset?

    a) EA = BA + Offset
    b) EA + BA = Offset
    c) EA = BA * Offset
    d) Offset + EA = BA
    e) None of the above

 

 

 

 

-----Answers-----

 

A. 1.- An array is s set of memory locations which are giving in a continuous manner. In the example below we can see how an array would allocate a set of  5 contiguous locations of memory for integers. 

 

example

 

 

A. 2. - An Offset is the distance from the first assigned location in RAM of the Array in question and to determine an offset we need two elements
1. The base address of the array (the location in memory where the array begins)
2. The array offset (often also referred to as the subscript or index)
once we have those elements we can use the formula EA = BA + Offset

 

 

A.3. - An illegal offset is when we assign an Array of  X elements then we reference a location outside those locations requested, X > 1 or more.

 

 

A. 4. - E is the right answer, the %p is a pointer, it only shows us the locations it doesn't assign a value. The %lu is also a pointer but it gives us the information in a decimal value.

 

 

A.5. - C is the right answer since:

Data Type

# of Bytes

char

1

short

2

int

2

long 

4

float

4

double

8

long double

16

 

 

A. 6. - A is the right answer since the formula is: Element Address = Base Address + Offset a.k.a. EA = BA + Offset