Arrays:
- By definition an array is "a data
structure which contains a fixed number of contiguous
- storage elements all of the same
data type."
Some of the data types
that can be stored are integers, longs, and floats. For our illustration
purposes we will be discussing integers.
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 |
Here
are some good Array references:
-
http://www.4duk.com/support/corners/filemaker/arrays.html
-
http://computer.howstuffworks.com/c4.htm
-
http://www.4d.com/docs/CMU/CMU10113.htm
Questions:
-
How many bytes of storage are needed for a
float? ANSWER: 4 Bytes
-
On the offset, is the variable stored always 0?
True or False ANSWER: True
|