Kislay wrote:
>
The individual units of the structure occupy the number of bytes they
are supposed to , and the extra bytes , if any are the padded ones .
So far so good . But the padded bytes will never be free to be used
for anything else , right ? So it can be said that on the whole the
structure is occupying the actual bytes plus the padded ones . The
padded bytes go waste . Consider the following structure ,
struct s
{
char c[2];
double d;
};
c occupies 2 B , d occupies 8 & the structure s occupies 16 (6 padded
bytes) . If these padded bytes cannot be put to any use , isn't it a
big waste of memory . Is structure padding really worth that ?
Define "worth it".
Consider a platform with a 64-bit memory bus. That is, all access
to memory is done 8 bytes* at a time. Consider further that, should
a read to a non-aligned double be performed, this would take two
reads from memory rather than one. Consider further still, that a
write to a non-aligned double would, rather than being just a single
write, need two reads plus two writes. That's at least a 100%
performance hit on reads, and 300% on writes.
Next, consider a similar platform which, rather than allowing the
non-aligned access, instead causes a hardware exception. On such a
platform, this probably means any program using such a construct
would crash. On other platforms, an exception handler takes over
and allows the access to happen, by executing code which reads the
two aligned 8-byte values and pulls out the appropriate bytes to
be assembled and returned. Or, for writes, reads the two values,
combines the bytes as needed, and writes both values back out.
Imagine the performance hit on this platform. It is quite easy to
forsee a 10,000% performace hit or more.
(Note: I have worked on platforms with all three of the above
scenarios, though not necessarily 8-byte alignment. And note, too,
that except for the "crash on non-aligned values" system, the C
compilers I used had an option to change the alignment used.)
So, once again, define "worth it".
* - Okay, so I'm assuming 8-bit bytes here. Sue me.
--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody |
www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net |
www.fptech.com | <std_disclaimer .h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th***** ********@gmail. com>