为什么在使用gcc时在Linux和Windows上打包结构的大小会不同? [英] Why would the size of a packed structure be different on Linux and Windows when using gcc?

查看:169
本文介绍了为什么在使用gcc时在Linux和Windows上打包结构的大小会不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,为什么在使用gcc编译时在Linux和Windows上打包结构的大小不同?

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版本:

$ 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)

编译和测试:

$ 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.

Linux二进制打印预期的7字节大小,Windows二进制8字节。为什么有区别?

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

推荐答案

第6.37.3节的gcc属性将它解释为ABI规范的区别, a href =http://gcc.gnu.org/onlinedocs/gcc/Type-Attributes.html =nofollow> http://gcc.gnu.org/onlinedocs/gcc/Type-Attributes.html

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

这篇关于为什么在使用gcc时在Linux和Windows上打包结构的大小会不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆