![]() |
![]() |
![]() |
![]() |
![]() |
|
The number of bytes allocated for each place in an array is determined by
the objects to be stored. Suppose, for example, that you define the
trade structure like this:
struct trade {double price; int number;};
Then, if an array is to hold trade objects, each of which contains a
floating-point number of type double for the trade object's
price and an integer of type int for the trade's number, each place
will consist of 12 bytes in most implementations:
0 <-- Array --> 1
| index |
v v
----------------------- -------------------------
price number price number
| | | |
v v v v
--------------- ------- ----------------- -------
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
| | | | | | | | | | | | | | | | | | | | | | | | | | |
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
^ ^ ^ ^
| | | |
100 108 112 120
In this example, the first trade is described by the 12 bytes at memory addresses 100 through 111. Of these 12 bytes, the first 8 bytes contain a floating-point number that represents the trade's price, and the final 4 bytes contain an integer that represents the number of shares in the trade. The second trade is described by the 12 bytes starting at memory address 112.