Hi all:

It seems like __attribute__((__packed__)) is not packing correctly, or at least 
not in the same way as #pragma pack in compiler version 0x590.  Is this a bug, 
or am I doing something wrong?

thanks
Mark

Given this program:

#include <unistd.h>
#include <stdio.h>
#include <stddef.h>

struct foo_unpacked  {
        uint32_t        f1;
        uint64_t        f2;
        uint32_t        f3;
        uint64_t        f4;
};

struct foo_packed_attribute  {
        uint32_t        f1;
        uint64_t        f2;
        uint32_t        f3;
        uint64_t        f4;
} __attribute((__packed__));

#pragma pack(1)
struct foo_packed_pragma  {
        uint32_t        f1;
        uint64_t        f2;
        uint32_t        f3;
        uint64_t        f4;
};
#pragma pack()

#define DUMP_STRUCT(s) \
        printf("sizeof(" #s ")=%d\n", sizeof(s)); \
        printf("offsetof f1=%d\n", offsetof(s, f1)); \
        printf("offsetof f2=%d\n", offsetof(s, f2)); \
        printf("offsetof f3=%d\n", offsetof(s, f3)); \
        printf("offsetof f4=%d\n", offsetof(s, f4));

int
main(int argc, char **argv)
{
        printf("__SUNPRO_C=%x\n", __SUNPRO_C);
        DUMP_STRUCT(struct foo_unpacked);
        DUMP_STRUCT(struct foo_packed_attribute);
        DUMP_STRUCT(struct foo_packed_pragma);
}


I get this output:

psg-solaris-01$ cc -m64 -o foo foo.c
psg-solaris-01$ ./foo
__SUNPRO_C=590
sizeof(struct foo_unpacked)=32
offsetof f1=0
offsetof f2=8
offsetof f3=16
offsetof f4=24
sizeof(struct foo_packed_attribute)=32
offsetof f1=0
offsetof f2=8
offsetof f3=16
offsetof f4=24
sizeof(struct foo_packed_pragma)=24
offsetof f1=0
offsetof f2=4
offsetof f3=12
offsetof f4=16
-- 
This message posted from opensolaris.org
_______________________________________________
tools-compilers mailing list
[email protected]

Reply via email to