4 ms·
The C standard provides guarantees about data layout even if the compiler can prove it will never be used internally, for the sake of, say, external debugging.
by Blikkentrekker 5y ago
The C standard provides guarantees about data layout even if the compiler can prove it will never be used internally, for the sake of, say, external debugging.
- gpderetta 5y agoAnd C compilers routinely optimize structures in such a way that they are impossible to inspect in a debugger. Because of SRA, non escaping aggregates might even not be allocated on a memory location at all.
- Blikkentrekker 5y agoWhat is an "escaping aggregate"?; this term has 80 hits on Google, none of which seem to be related to programming. Looking it up “aggregate” is a C++ term for which the standard indeed does explicitly allow various optimizations and guarantees about data layout are weakened; this is not the case with C arrays. Do you have any practical evidence to C compilers altering the data layout of an array in any way?
- throwaway17_17 5y agoGP’s comment is referring to Scalar Replacement of Aggregates on the one hand. This is an optimization where by the compiler will, for a function with a Struct argument, only pass in a pointer to / copy of the actual element of the struct used inside a function. Hence the name, the compiler is replacing the aggregate struct by one or more of the individual values it contains. The reference to non-escaping aggregates, describes the compiler optimizing a local only struct by keeping the individual elements in registers during usage, and because the struct as a whole does not leave the local scope, these elements are just discarded when that scope closes. Both of these transformations/optimizations are performed by all three industrial C and C++ compilers (gcc/llvm/msvc).
- gpderetta 5y agoExactly. Thanks.
- astrange 5y agoIt doesn't require this as long as the program has the same side effects - the contents of memory are not a side effect necessarily. The problem is that there are lots of operations which imply the existence of part of an object, like passing around pointers or allocating single objects at a time, and that would disable these layout optimizations. An optimization that's easy to break isn't useful even if it would help a lot; you want them to be predictable more than anything.
- lostmsu 5y agoWhich sections of the standard are you referring to?