Why is there no heap overhead for the base reference?

  • A+

From Stroustrup's Foundations of C++, he offers a pure object-oriented language (on Page 4).

class complex { double re, im; /* … */ }; complex a[ ] = { {1,2}, {3,4} }; 

He assumes a in the pure object-oriented language is allocated on the heap, and a's memory layout looks like: Why is there no heap overhead for the base reference?

The likely size is 3*sizeof(reference)+3*sizeof(heap_overhead)+4*sizeof(double). Assuming a reference to be one word and the heap overhead to be two words, we get a likely size of 19 words to compare to C++’s 8 words. This memory overhead comes with a run-time overhead from allocation and indirect access to elements. That indirect access to memory typically causes problems with cache utilization and limits ROMability.

I noticed that the uppermost reference has no heap overhead (white rectangle).

I guess it is a general phenomenon rather than specified for the pure OO example language.

But I cannot find any reference (I do admit this is not a search-engine-friendly question).


Thanks for your answers. However, I forgot to post my original guess. (Apologies, it is my fault.)

Actually, I also thought because a itself may be allocated on the stack or somewhere else, it will not have heap overhead itself. But later, I noted BS also says:

Compare this with a more typical layout from a “pure object-oriented language” where each user-defined object is allocated separately on the heap and accessed through a reference...

on Page 4.

So, I think that he has limited the implementation to heap-only (or stack-less).

(Of course, maybe I'm reading too much into this sentence.)

You get heap overhead when you store something on the heap. The two complex values are stored on the heap, so they get overhead. The array of references is also stored on the heap, so it gets overhead.

However, the reference to the array is not stored on the heap. Usually, this reference will be either placed on the stack as a local variable, or that stack storage might be optimized away by using a CPU register instead. In either case, the reference itself is just a local pointer variable which does not have heap allocation overhead itself.


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: