Home Segments Index Top Previous Next

378: Mainline

Now consider the memory consumed by the old and new versions of the total-value–computing programs. The old version, using an array of 100 trades, each of which consumes, say, 12 bytes, uses 1200 bytes no matter how many trades 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 12 bytes for each trade read from a file.

If 100 trades are read from a data—the worst case—the total memory consumed by the new version, the one that uses pointers, is 1600 bytes, which is more than the 1200 required by the old version. On the other hand, if only 10 trades are read, the total memory consumed by the new version is only 400 + 10 * 12 = 560, which is less than the 1200 required by the old version.

Thus, you can save a great deal of memory if the expected number of structure objects is much less than the worst-case number of structure objects you need. Your saving increases as the size of each structure object grows relative to the size of each pointer.