GCC VS MSVC类的压缩和对齐 [英] GCC vs MSVC class packing and alignment

查看:567
本文介绍了GCC VS MSVC类的压缩和对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否与编译器标志的方式来创建一个基类,并根据MSVC派生类相同的内存布局相比,它相当于在gcc?使用VS​​2010并将其与GCC 4.1.1即时通讯

Is there a way with compiler flags to create the same memory layout of a base class and a derived class under msvc compared to its equivalent in gcc? Im using VS2010 and comparing it to GCC 4.1.1

于是

#if define _MSVC
// window 
DALIGN(A,B) __declspec(align(A)) B
#else
// GCC
DALIGN(A,B) B __attribute__((aligned(A)))
#endif
class A
{ 
  DALIGN(CVector V,16);
  int a;
}
class B : public A
{
  int b;
}

一个是16byte对齐从而为0x20(32) B也是16字节对齐,但可能也为0x20(32)在GCC但MSVC它的0x30(48)

A is 16byte aligned making it 0x20(32) B is also 16 byte aligned, but is either also 0x20(32) on GCC but on msvc it is 0x30(48)

有没有办法强制MSVC收拾数据,如海湾合作委员会是?

is there a way to force the msvc to pack the data like GCC is?

这样做的原因:我创建的数据,并直接加载到不同平台上的类。怎样才是真正是很好的,如果我可以使用完全相同的数据布局在两个平台上。 (是的尾数是一样的在两个平台上)

Reason for this: I create data and load it directly into the classes on different platforms. What would really be nice is if I could use the exact same data layout on both platforms. (Yes endian is the same on both platforms)

推荐答案

添加的#pragma包(推,4)/的#pragma包(POP) 周围的A类和B类成功的正确对齐数据GCC对齐了。

Adding #pragma pack(push, 4) / #pragma pack(pop) around the class A and class B succeeded in correctly aligning the data as GCC aligns it.

请注意,如果继承被延长,这似乎是任何需要对准类和向前都需要包装的继承。 例如类A,B,C,D,E,F C已经对准成员 C,D,E,F都需要身边的包。

Note that if the inheritance is extended, it seems like for any required aligned class and forward through the inheritance all require the packing. Example classes A,B,C,D,E,F C has aligned members C,D,E,F all require the packs around them.

这篇关于GCC VS MSVC类的压缩和对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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