Portable __attribute__ ((__packed__))

Is there a portable equivalent __attribute__ ((__packed__))

(for mobility, I mean working on PC, Android and iPhone, regardless of architecture)

+3
source share
2 answers

By definition, __attribute__they are not tolerated. If you want your data to be in a specific way, the Plan9 solution usually uses arrays unsigned charin your structure:

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

Usually there will be no alignment, since it mainly uses unsigned characters. Of course, if you have high pain tolerance, you can always use enumbiases:

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

Then you can download it like this:

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 a stomach, it will be fully portable.

+4
source

. ++ ( ABI), , , .

+1

All Articles