portable __attribute__ ((__packed__))

c, c++, portability

Solution

By definition, `__attribute__`s are non-portable. If you want your data a specific way, the Plan9 solution is usually to use `unsigned char` arrays in your structure:

struct foo {
    uchar data1[4];
    uchar other[2];
}

Usually won't have any alignment, since it's fundamentally using unsigned characters. Of course, if you have a high tolerance for pain, you could always use an `enum` of offsets:

enum { FOO_DATA1=0, FOO_OTHER=4, FOO_LAST=6 };

You could then load it up like so:

unsigned char foo[FOO_LAST];
uint32_t n = htonl(val)
memcpy(foo+FOO_DATA1, &n, sizeof n)
memcpy(foo+FOO_OTHER, &some_16_bit_val, sizeof some_16_bit_val)

As long as you have the stomach for it, it will be completely portable.

Problem

Is there a portable equivalent of `__attribute__ ((__packed__))` (by portability I mean working on PC, Android and iPhones, regardless architecture)

Original source