On the stack the compiler is free to do a lot of optimizations, because the context is static and known at compile time, but when dealing with access to dynamically allocated objects and generally accessing "by reference" the context is not known, so logically in such cases, member access boils down to dereferencing a memory address derived by adding the object base address and the offset for that member. Or does it? I am pretty new to this, so at this point I am only guessing and probably missing a lot of details.
For example I noticed that if I implement the +
operator as void add(int * a, int * b, int * r);
when working with stack members (and using the &
operator) the assembly code is identical to that the regular +
operator creates, so it does seem that pointers which are known to point to compile time known values are are being optimized to exclude extra dereferencing (and copying) and the indirections inside the add()
function are inlined as direct access to stack objects. Does this mean the compiler is "good enough" to be able to optimize say accessors implemented with indirection from constant value offsets from the object base address on the stack as if using struct member access to get that value?