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

How arrays are stored in RAM?

  

ARRAY : A data structure containing a fixed number of contiguous storage elements all of the same type.

 

 

Up to now, the only variables we have used could only one store one value at a time. However, many applications require that many values be stored simultaneously.

 

 

All the elements of an array must be the same data type, for example float, char, int, pointer to float, pointer to int, a structure or function.

 

 

Like any other variables, arrays must be declared and created before they can be used.

The creation of arrays involves three steps:

Declare the array

Initialize the storage area

Put values into the array

 

 

 

 

 

 

 

Declaration

 

 

Type arrayname[] or type [] arrayname

 

Ex: int students [] or int[] students.

 

An array is declared in the following manner: int x [10]

This reserves space in memory for 10 integers. We refer these integers as follows: x[0], x[1], ..., x[9]. Note that the first entry in an array is always indexed with a 0.

Using the data type int implies that we will require 2-bytes per element

 

 

 

 

Initialization

 

 

An array is initialized to 0, false or null depending upon the type

The array index starts at position 0.

An array of 10 elements has index 0 to 9.

We can not access positions not within the range

Each value is stored at a specific position.

Example: To store the heights of 6 people, we could name the heights: h1, h2, …, h6, or we can ask the compiler to allocate six memory cells to h; identified as: h[0], h[1], h[2], h[3], h[4], h[5]. 

 

 

Because we are specifying only one subscript or offset, we are declaring a vector (a one-dimensional array)

 

 

Arrays can also be initialized like standard variable at the time of their declaration.

Type arrayname[]={list of values}

Ex : int[] students = {55,69,70,30,80}

 

Values into the array                                                            

 

Once arrays are created, they need to be initialized with some values before access their content.

For example, students [0] =50

                      Students [1] =40                       

Arrays are fixed length. Length is specified at create time.

 

 

 

 

 

The storage in RAM depends of the type of data.

 

In C++ when we store integers, they require 2 bytes per number we store.  For large amounts of information storage in memory, the computer must determine:

 

 

What type of data is being stored?

Where in memory is there a appropriate section of contiguous RAM?

Say we want to store an array that has the first 9 even numbers; we actually need 20 bytes of storage. That is because we also need 2 bytes for the offset.  If there was 20 bytes of contiguous storage starting at 5000, the data would be listed as:

 

Variable                            Address                        Decimal Value        

even1                                 5000/5001                            0

even2                                5002/5003                             2

even3                                5004/5005                             4

even4                                5006/5007                             6

even5                                5008/5009                             8

even6                                5010/5011                            10

even7                                5012/5013                            12

even8                                5014/5015                            14

even9                                5016/5017                            16

even10                               5018/5019                           18

 

The result in RAM would look like the following:

5000 (even[0])

5001 (even[0])

5002 (even[2])

5003 (even[2])

5004 (even[4])

00000000

00000000

00000010

00000010

00000100

5005 (even[4])

5006 (even[6])

5007 (even[6])

5008 (even[8])

5009 (even[8])

0000010

00000110

00000110

00001000

00001000

5010 (even[10])

5011 (even[10])

5012 (even[12])

5013 (even[12])

5014 (even[14])

00001010

00001010

00001100

00001100

00001110

5015 (even[14])

5016 (even[16])

5017 (even[16])

5018 (even[18])

5019 (even[18])

00001110

00010000

00010000

00010010

00010010

 Notice:

         The first element on the list (element number 1) has the subscript (offset) 0 (zero)

         The last element on the list (element number 10) has the subscript (offset) 9 (nine).

 

Some good references include:

http://www.cprogramming.com/tutorial/lesson8.html

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/modcore/html/deconunderstandingarrays.asp

 

And now, test your knowledges…                                        

Questions

1-      How many bytes does an integer require per number we store?

2-      What is the number of initialization of an array?

Answers

1-      2

2-      0