Home Segments Index Top Previous Next

384: Mainline

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 cylinder class like this:

class cylinder {public: double radius, length;}; 

Then, if an array is to hold cylinders, each of which contains floating-point numbers of type double for the cylinder's radius and length, each place will consist of 16 bytes in most implementations:

                    1         <-- Array -->         2 
                    |             index             | 
                    v                               v 
     ------------------------------- ---------------------- 
          radius          length          radius 
            |               |               | 
            v               v               v 
     --------------- --------------- --------------- ---- 
 -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*- 
    | | | | | | | | | | | | | | | | | | | | | | | | | | 
   -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*- 
     ^               ^               ^               ^ 
     |               |               |               | 
    112             120             128             136 

In this example, the first cylinder is described by the 16 bytes at memory addresses 112 through 227. Of these 16 bytes, the first 8 bytes contain a floating-point number that represents the cylinder's radius, and the second 8 bytes contain a floating-point number that represents the cylinder's length. The second cylinder is described by the 16 bytes starting at memory address 128.