C ++结构大小不等于C ++结构大小在C ++ / CLI? [英] C++ Struct Size unequal C++ Struct Size in C++/CLI?

查看:167
本文介绍了C ++结构大小不等于C ++结构大小在C ++ / CLI?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是指标题中提到的问题。



我在头文件中有一个简单的结构,如下所示:

  typedef struct 
{
WORD FileType; //文件ID(0x7000)
WORD HeaderSize; //这个文件头的大小(Bytes)
WORD HeaderVersion; // yy.y
ULONG FileSize; //整个文件的大小(Bytes)
WORD ImageHeaderSize; //以字节为单位的图像头的大小
WORD ULX,ULY,BRX,BRY; //图像的边界矩形
WORD NrOfFrames; // self explanatory
WORD校正; // 0 = none,1 = offset,2 = gain,4 = bad pixel,(ored)
double IntegrationTime; //帧时间(以微秒为单位)
WORD TypeOfNumbers; // short,long integer,float,signed / unsigned,inverted,
//故障映射,偏移/增益校正数据,坏像素校正数据
BYTE x [WINRESTSIZE]; // fill up to 68 byte
} WinHeaderType;

如果我在C控制台应用程序中调用sizeof(WinHeaderType),我得到68,
如果我在C ++ / CLI中使用相同的参数调用相同的函数,我得到80。



有人可以向我解释这个行为吗?



我是C ++ / CLI的新手,事实上我从来没有使用过
(几天前开始),但我需要为.NET应用程序创建一个dll。 p>

由于这是我的第一篇文章,我希望我没有违反任何论坛规则。



Greez,
Mo



只是变得陌生和陌生...





第一行:C控制台应用程序输出



第二行,第一列:C / C ++控制台应用程序我编码为检查DataType大小。



第二行,第二列:C ++ / CLI控制台应用程序I已编码为检查DataType大小。



第一个应用程序的来源: http://fbe.am/sId



最后两个应用程序的项目文件: http://fbe.am/sIf



有人解释这个吗?


使用默认打包规则(/ Zp8),该结构包含12个字节的填充以使成员对齐。这应该是足够好,以解释68和80之间的差异。你必须消除填充,使用 #pragma pack 在MSVC编译器:

  #pragma pack(push,1)
typedef struct {
// .. etc
} WinHeaderType ;
#pragma pack(pop)


my question refers to the problem mentioned in the title.

I have a simple struct in a header file that looks like this:

typedef struct
{
    WORD FileType;          // File ID (0x7000)
    WORD HeaderSize;        // Size of this file header in Bytes
    WORD HeaderVersion;     // yy.y
    ULONG FileSize;         // Size of the whole file in Bytes
    WORD ImageHeaderSize;   // Size of the image header in Bytes
    WORD ULX, ULY, BRX, BRY;// bounding rectangle of the image
    WORD NrOfFrames;        // self explanatory
    WORD Correction;        // 0 = none, 1 = offset, 2 = gain, 4 = bad pixel, (ored)
    double IntegrationTime; // frame time in microseconds
    WORD TypeOfNumbers;     // short, long integer, float, signed/unsigned, inverted, 
                            // fault map, offset/gain correction data, badpixel correction data
    BYTE x[WINRESTSIZE];        // fill up to 68 byte
} WinHeaderType;

If I call sizeof(WinHeaderType) in a C console application I get 68, but if I call the same function with the same parameter in C++/CLI I get 80.

Could someone explain this behaviour to me?

I'm pretty new to C++/CLI, in fact I never worked with it before (started a few days ago), but I need to create a dll for .NET applications.

Since it's my first post I hope I did not break any of the forum rules.

Greez, Mo

[EDIT] It just becomes stranger and stranger ...

First Row: C Console Application Output

Second Row, First Column: C/C++ Console Application I coded to check DataType sizes.

Second Row, Second Column: C++/CLI Console Application I coded to check DataType sizes.

Source of the first application: http://fbe.am/sId

Project Files of the last two applications: http://fbe.am/sIf

Someone an explanaition for this?!

解决方案

With default packing rules (/Zp8), the structure contains 12 bytes of padding to get the members aligned. Which ought to be good enough to explain the difference between 68 and 80. You'll have to eliminate the padding, use #pragma pack in the MSVC compiler:

#pragma pack(push, 1)
typedef struct {
   // .. etc
} WinHeaderType;
#pragma pack(pop)

这篇关于C ++结构大小不等于C ++结构大小在C ++ / CLI?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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