I'm working on some C++ code for an embedded system. The I/O interface the code uses requires that the size of each message (in bytes) is a power of two. Right now, the code does something like this (in several places):
#pragma pack(1)
struct Message
{
struct internal_
{
unsigned long member1;
unsigned long member2;
unsigned long member3;
/* more members */
} internal;
char pad[64-sizeof(internal_)];
};
#pragma pack()
I'm trying to compile the code on a 64-bit Fedora for the first time, where long
is 64-bits. In this case, sizeof(internal_)
is greater than 64, the array size expression underflows, and the compiler complains that the array is too large.
Ideally, I'd like to be able to write a macro that will take the size of the structure and evaluate at compile time the required size of the padding array in order to round the size of the structure out to a power of two.
I've looked at the Bit Twiddling Hacks page, but I don't know if any of the techniques there can really be implemented in a macro to be evaluated at compile time.
Any other solutions to this problem? Or should I perpetuate the problem and just change the magical 64 to a magical 128?
Probably the most obvious way would be to just use the ternary operator:
And yet another template solution (robbing hugely from fizzer):
My approach is simply to keep doubling the padding size until it's at least as big as the type size.
Why not use a union?
or better yet use anonymous structs
So you can access members like this: Message.member1;
Edit: obviously this doesn't solve your greater than 64 problem, but provides a cleaner way of padding.
Would be a little cleaner.
You're already using
#pragma pack
, I don't know what compiler(s) your using specifically, but you should see if they support arguments for pack that control the alignment/padding and then you can just get rid of the padding field altogether. I know MSVC's version ofpragma pack
supports this, as does GCC's.I like Niki's answer, especially the part with anonymous structs.
One thing that answer didn't solve was the greater-than-64-bytes problem, but that can be solved by conditionally declaring a char[128] struct member if sizeof(long)==8 and declaring char[64] otherwise.