Variable padding to alignment boundaries #1220
Unanswered
Teuxe
asked this question in
Pattern Language Q&A
Replies: 2 comments
-
Hey #include <std/mem.pat>
struct MyString {
char value[];
std::mem::AlignTo<8>;
}; This will include one null-byte in the #include <std/mem.pat>
struct MyString {
char value[while($[$] != 0x00)];
std::mem::AlignTo<8>;
}; |
Beta Was this translation helpful? Give feedback.
0 replies
-
For any offset and alignment power of two the number of bytes needed to pad to the next aligned address is -offset & (align - 1) this assumes offset is unsigned or 2's complement arithmetic is used. Similarly, the next aligned address can be calculated using (offset + (align - 1)) & -align |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi,
I have not found how to have a struct containing a null-terminated char[] and also containing NUL bytes as padding to the next 32-bit boundary (in other file types, this boundary could be every 16 bits, every 64 bits...). Say it otherwise: in a size where you could put 32-bit words (we don't know how many), there is a null-terminated char inside whose size is determined by the first NUL byte. The padding is not necessarily filled with NUL (but usually is).
Examples with 32-bit alignment:
48 65 79 00 DE AD BE EF 55 AA 55 AA
H e y . . . . . 7 . 7 .
^ ^ ^
string...\0 aligned data
(no padding)48 65 6C 6C 6F 00 00 00 DE AD BE EF 55 AA 55 AA
H e l l o . . . . . . . 7 . 7 .
^ x x ^ ^
string... \0 padd. aligned data
(2 bytes for padding)48 65 6C 6C 6F 21 21 00 DE AD BE EF 55 AA 55 AA
H e l l o ! ! . . . . . 7 . 7 .
^ ^ ^
string... \0 aligned data
(no padding)48 65 6C 6C 6F 20 79 6F 75 21 00 00 DE AD BE EF 55 AA 55 AA
H e l l o y o u ! . . . . . . 7 . 7 .
^ x ^ ^
string... \0 p. aligned data
(1 byte for padding)Is there currently a way to do that?
Thanks,
Beta Was this translation helpful? Give feedback.
All reactions