Say I have a collection of data (eg: strings) that must be stored in a binary file padded so that each string is, say, 4-byte aligned.
So if I have a string of length 11, it would get padded to 12 (with null bytes).
If I have a string of length 24, then no padding is necessary.
If my string has a length of 6, it would get padded to 8 bytes.
How do I compute the amount of padding required in a single expression?
I tried 4 - (string_length % 4)
but it fails when my string length is a multiple of 4.
For instance, in a 32-bit architecture, the data may be aligned if the data is stored in four consecutive bytes and the first byte lies on a 4-byte boundary. Data alignment is the aligning of elements according to their natural alignment.
structure A If the short int element is immediately allocated after the char element, it will start at an odd address boundary. The compiler will insert a padding byte after the char to ensure short int will have an address multiple of 2 (i.e. 2 byte aligned).
Data structure alignment is the way data is arranged and accessed in computer memory. Data alignment and Data structure padding are two different issues but are related to each other and together known as Data Structure alignment.
In short, Yes!! On my Windows system, a union is padded to have a size that is a multiple of the size of an int ,ie 4 bytes.So unless you use #pragma pack(1) , a union having size 3 bytes will be padded with one more byte to yield 4 bytes.
This looks odd but gives the right answer:
(4 - (string_length % 4)) % 4
There is a faster way to compute the padding, if the alignment is a power of two (2,4,8,...). The following runs because binary & is similar to % for powers of two: %(2^x)
and &(2^x-1)
do the same for positive numbers. Attention: & will delete the sign bit and therefore always returns the positive modulo result.
So (4 - (string_length & 3)) & 3
will do the same as (4 - (string_length % 4)) % 4
. Using the positive modulo property this can be simplified to
(-string_length) & 3
!
If you wanna add that result to the size you can even do more optimizations:
padded_length = (string_length + 3) & ~3
Semantically this 'rounds up' the number to the padding size of 4.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With