Home Segments Index Top Previous Next

435: Mainline

Now consider the memory consumed by the old and new versions of the volume-computing programs. The old version, using an array of 100 cylinders—each of which consumes, say, 16 bytes—uses 1600 bytes, no matter how many cylinders are read from a file. The new version, using an array of 100 pointers—each of which consumes, say, 4 bytes—uses 400 bytes for pointers plus 16 bytes for each cylinder read from a file.

If 100 cylinders are read from a file—the worst case—the total memory consumed by the new version, the one that uses pointers, is 2000 bytes, which is more than the 1600 required by the old version. On the other hand, if only 10 cylinders are read, the total memory consumed by the new version is only 400 + 10 * 16 = 560, which is much less than the 1600 required by the old version. Thus, you can save a great deal of memory if the expected number of class objects is much less than the worst-case number of class objects you need. Your saving increases as the size of each class object grows relative to the size of each pointer.