Search code examples
iosgcccompiler-errorspadding

structure padding compile flag


typedef struct{
    char        t1;
    long long   t2;
    char        t3;
}struct_size_test;

printf("sizeof(long long)==[%ld]\n", sizeof(long long));
printf("sizeof(char)==[%ld]\n", sizeof(char));
printf("sizeof(struct_size_test)==[%ld]\n", sizeof(struct_size_test));

The result of struct_size_test in iOS and in OS X gcc is different.
The result in iOS is

sizeof(long long)==[8]
sizeof(char)==[1]
sizeof(struct_size_test)==[24]

But the result in OS X gcc is

sizeof(long long)==[8]
sizeof(char)==[1]
sizeof(struct_size_test)==[16]

I googled this and found some advice that to add "-malign-double" as the gcc compile flag. But it shows

clang: error: unknown argument: '-malign-double' [-Wunused-command-line-argument-hard-error-in-future]
clang: note: this will be a hard error (cannot be downgraded to a warning) in the future

Any idea how to sync the structure padding for iOS and OS X? Thanks a lot!


Solution

  • I think the answer should be that you most certainly don't want to do this.

    You could try nneonneo's solution or add __attribute__((packed)) to your structs to make the structs the same size on both OS X and iOS but keep in mind this is a compiler specific feature and will make your code less portable

    Furthermore, if you did change the padding, your program would likely crash or run less efficiently on either architectures (especially iOS/ARM) due to alignment requirements not being met. Some ARM architectures (unsure about iDevices) won't allow unaligned reads/writes and will outright crash if not run slower (since two memory reads are required instead of one).

    A workaround might be to use the highest common alignment (appears to be 8), which would simply take a bit more space. But this is still hacky and non-portable.

    Why are you trying to do this anyway?

    Edit: nneonneo's answer probably should look like this:

    typedef struct{
        char        t1 __attribute__ ((aligned (8)));
        long long   t2 __attribute__ ((aligned (8)));
        char        t3 __attribute__ ((aligned (8)));
    } struct_size_test;