-
structure packing
hi,
structure padding is the way to put padding bits/bytes depening on byte boundary.
but where structure packing comes, as i know that it supresses the padding bits.if it so, is the processor able to read that paricular memory efficiently.
supressing means what actually will happen in memory.
Code:
struct
{
char a;
char b;
char c;
int i;
};
--->here 1 byte+1 byte + 1 byte + padding byte + 4 bytes = 8 bytes
sturct
{
char a;
char b;
char c;
int i;
}__attribute__((packed));
--->here 1 byte+1 byte+1 byte+4 bytes=7 bytes.
supressing of padding bytes means what?.but then no byte boundary here.
please tell me what is it?
-
Some CPUs, particular from Intel, don't have to have everything lined up along boundaries, although performance significantly suffers.
This is a rather system specific and even compiler specific topic.
-
Right, the padding is there for a reason:
reading an int from an address that is aligned with the size of the int makes it much faster.
On some CPU's it's even invalid to read unaligned data types - so the member "i" in your packed struct could not be read directly on a 68000 machine, for example, an attempt to do so would trap into a "unaligned access" handler - which of course COULD fix up the problem, but now reading a single integer, which is a relatively simple operation, is taking hundreds of clock-cycles. Most machines with this type of architecture will CRASH when an unaligned access occurs, rather than fix it up [because it's probably not intentional].
There are good reasons to pack data structures for compatibility with old architectures, but it's bad for performance and portability.
--
Mats
-
thank you for reply. from this i got something on structure padding and packing