struct padding在c ++中 [英] struct padding in c++

查看:234
本文介绍了struct padding在c ++中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在c ++中有一个 struct ,是否没有办法安全地读/写一个跨平台/编译器兼容的文件?因为如果我理解正确,每个编译器会根据目标平台有所不同。

解决方案

否。这是不可能的。这是因为 C ++在二进制级别缺乏标准化



Don Box 写道(引自他的书 Essential COM ,第 COM As A Better C ++


C ++和可移植性





一旦对
做出决定,将一个C ++类作为DLL分发,一个
面临着 c ++的基本的
弱点之一 strong>,即在二进制级别缺少
标准

虽然ISO / ANSI C ++草案
工作论文试图编纂哪些
程序将编译和运行

语义效果
它不试图标准化
C ++的二进制运行时模型

第一次这个问题将变成
明显是当客户端尝试链接
对FastString DLL的导入库从
a C ++开发环境其他
用于构建
FastString DLL的文件。


struct-padding由不同的编译器完成。即使您使用相同的编译器,结构的打包对齐可能会根据

不仅如此,如果你写的两个结构的成员都是完全相同的 的区别在于它们的声明顺序是不同的,那么每个结构体的大小可以不同(通常是不同的)。



例如,参见

  struct A 
{
char c;
char d;
int i;
};
struct B
{
char c;
int i;
char d;
};
int main(){
cout<< sizeof(A)< endl;
cout<<尺寸(B) endl;
}

编译 gcc-4.3.4 ,您将获得此输出:

  8 
12



也就是说,即使两个结构都具有相同的成员,大小也不同。



Code at Ideone: http://ideone.com/HGGVl



底线是标准没有谈到填充应该如何做,因此编译器可以自由做出任何决定,你不能假设所有编译器做出相同的决定。


If I have a struct in c++, is there no way to safely read/write it to a file that is cross-platform/compiler compatible? Because if I understand correctly, every compiler 'pads' differently based on the target platform.

解决方案

No. That is not possible. It's because of lack of standardization of C++ at the binary level.

Don Box writes (quoting from his book Essential COM, chapter COM As A Better C++)

C++ and Portability


Once the decision is made to distribute a C++ class as a DLL, one is faced with one of the fundamental weaknesses of C++, that is, lack of standardization at the binary level. Although the ISO/ANSI C++ Draft Working Paper attempts to codify which programs will compile and what the semantic effects of running them will be, it makes no attempt to standardize the binary runtime model of C++. The first time this problem will become evident is when a client tries to link against the FastString DLL's import library from a C++ developement environment other than the one used to build the FastString DLL.

struct-padding is done differently by different compilers. Even if you use the same compiler, the packing alignment for structs can be different based on what pragma pack you're using.

Not only that if you write two structs whose members are exactly same, the only difference is that the order in which they're declared is different, then size of each struct can be (and often is) different.

For example, see this,

struct A
{
   char c;
   char d;
   int i;
};
struct B
{
   char c;
   int i;
   char d;
};
int main() {
        cout << sizeof(A) << endl;
        cout << sizeof(B) << endl;
}

Compile it with gcc-4.3.4, and you get this output:

8
12

That is, sizes are different even though both structs has same members!

Code at Ideone : http://ideone.com/HGGVl

The bottomline is that the Standard doesn't talk about how padding should be done, and so the compilers are free to make any decision and you cannot assume all compilers make the same decision.

这篇关于struct padding在c ++中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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