18

In the code below, why is the size of the packed structure different on Linux and Windows when compiled with gcc?

#include <inttypes.h>
#include <cstdio>

// id3 header from an mp3 file
struct header
{    
        uint8_t version[ 2 ];
        uint8_t flags;
        uint32_t size;
} __attribute__((packed));

int main( int argc, char **argv )
{
        printf( "%u\n", (unsigned int)sizeof( header ) );
        return 0;
}

gcc versions used:

$ g++ --version
g++ (Ubuntu/Linaro 4.5.2-8ubuntu4) 4.5.2
$ x86_64-w64-mingw32-g++ --version
x86_64-w64-mingw32-g++ (GCC) 4.7.0 20110831 (experimental)

Compile and test:

$ g++ -Wall packed.cpp -o packed && ./packed
7
$ x86_64-w64-mingw32-g++ -Wall packed.cpp -o packed.exe
--> prints '8' when run on Windows.

The Linux binary prints the expected size of 7 bytes, the Windows binary 8 bytes. Why the difference?

9
  • 1
    My guess is the attribute somehow gets ignored (because of a bug). Use offsetof(header, size) to find out if it's so.
    – cnicutar
    Oct 17, 2011 at 5:26
  • 1
    If I compile it directly from windows xp with MinGW g++ 4.5.2, it prints 7. 32 bit though.
    – Vlad
    Oct 17, 2011 at 5:28
  • struct sizes varying is expected, which is why it is a good idea to never use/pass structures across compile domains.
    – old_timer
    Oct 17, 2011 at 5:28
  • 8
    Side note: Any C or C++ code that relies on specific struct member alignment (e.g. reading or writing structures directly to disk or network) smells bad...
    – 6502
    Oct 17, 2011 at 5:48
  • 1
    Meh, it only sounds bad. Oct 17, 2011 at 9:05

5 Answers 5

7

gcc 4.7.0 does it this way to be compatible with 64-bit MSVC++. If you want to pack the structure properly, compile with -mno-ms-bitfields. (But then your layout will be incompatible with MSVC++.)

4

Section 6.37.3 of the gcc attributes explains it as a difference in ABI specs, see here: http://gcc.gnu.org/onlinedocs/gcc/Type-Attributes.html

1
  • 1
    The microsoft compiler lets you pack the members as tight as you like. See: #pragma pack for the MS compiler.
    – harper
    Oct 17, 2011 at 5:40
3

The attribute((packed)) is compiler-specific to GCC. Hence, that code won't even compile with MSVC++. Maybe you used another compiler for Windows, though. However, with MSVC++ you could do this:

#include <stdint.h>
#include <cstdio>

// id3 header from an mp3 file
#pragma pack(push,1)
struct header
{    
        uint8_t version[ 2 ];
        uint8_t flags;
        uint32_t size;
};
#pragma pack(pop)

int main( int argc, char **argv )
{
        printf( "%u\n", (unsigned int)sizeof( header ) );
        return 0;
}

and the struct will be 7 bytes.

1
  • 5
    But it'll compile just fine using mingw under windows, which it appears the OP is using
    – Necrolis
    Oct 17, 2011 at 6:06
1

This is all about attribute and word alignment in memory

see if you write

struct header
{    
        uint8_t version[ 2 ];
        uint8_t flags;
        uint32_t size;
};

then linux & windows both have size 8

but when you specify attribute to avoid default world allignment then

struct header
{    
        uint8_t version[ 2 ];
        uint8_t flags;
        uint32_t size;
} __attribute__((packed));

then in linux because of attritube size becomes 7

see gcc spec says that

If packed is used on a structure, or if bit-fields are used 
it may be that the Microsoft ABI packs them differently than 
GCC would normally pack them. 
3
  • 1
    Perhaps because the compiler specific bit does not matter ? What matters is the quote about ABI. Oct 17, 2011 at 6:39
  • i have added that bcz user is going to compile program with attribute((packed)) in windows, attribute((packed)) is not supported in windows compiler Oct 17, 2011 at 6:48
  • 2
    "windows compiler"? OP specifies he uses GCC both on linux and Windows - MSVC isn't the only compiler around for Windows.
    – snemarch
    Oct 17, 2011 at 8:21
0

Update. Latest MinGW works fine.

Both g++ (i686-win32-dwarf-rev0, Built by MinGW-W64 project) 8.1.0 and g++ (x86_64-win32-seh-rev0, Built by MinGW-W64 project) 8.1.0 prints sizeof() of sample code is exactly equal 7 bytes.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.