Re:The Lost Art of C Structure Packing (2014)
by gaius on Friday August 05, 2016 @01:57PM
(#12234434)
What if your struct is actually a memory-mapped I/O device? Far from optimizing you need to give a hard guarantee that it will NEVER be optimized by the compiler.
[ Parent
| on HN ]
Re:The Lost Art of C Structure Packing (2014)
by kazinator on Friday August 05, 2016 @02:36PM
(#12234774)
Suppose C didn't require order of struct members. How could we support I/O devices? Simply by wrapping them behind functions or macros which take a base address and access a specific register relative to that address using pointer arithmetic and dereferencing. You see this being done anyway!
if ((SERIAL_STATUS_PORT(port_addr) & SERIAL_DTR) != 0) {
/* DTR line is asserted */
}
Re:The Lost Art of C Structure Packing (2014)
by gaius on Friday August 05, 2016 @04:21PM
(#12235480)
More likely you'd have to drop down into ASM for those bits...
You see in a lot of high performance networking code too, you just get a block of bytes off the wire, cast it to a struct and use it straight away. High risk, high reward :-)
Re:The Lost Art of C Structure Packing (2014)
by Grishnakh on Friday August 05, 2016 @02:38PM
(#12234789)
Well if we were making an all-new C-like language, we could just add a compiler directive which prevents optimization for cases like this.
For all I know, other languages may do just this (there's a comment farther down about Rust which appears to say this).