Visual Studio 6.0是否存在已知缺陷?见下面的代码 [英] Does Visual Studio 6.0 have a known defect? see code below

查看:98
本文介绍了Visual Studio 6.0是否存在已知缺陷?见下面的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我有一个.h文件,其中包含一个结构,用于定义在UDP多播套接字上发送的消息的标头。如果我手数计算结构的所有成员的字节,我得到16个字节,这是它应该是。



在我使用的代码中struct,当我对它执行sizeof()时,我得到20个字节。 4个字节太大。



微软是否添加了一些不明显的行李?



这是结构。



In my application I have a .h file that contains a struct which defines the header of a message that gets sent out on a UDP multicast socket. If I hand count the bytes of all the members of the struct I get 16 bytes, which is what it is supposed to be.

In my code where I use the struct, when I do a sizeof() on it I get 20 bytes. 4 bytes too large.

Does Microsoft add some baggage that isn't outwardly obvious??

Here's the struct.

struct message_header_struct 
{

	unsigned char msg_header3:8; 
	unsigned char msg_header2:8;
	unsigned char msg_header1:8;
	unsigned char msg_header0:8;
	unsigned char reserved1:8;
	unsigned char reserved2:8;
	unsigned short msg_size; 				

	unsigned char pri_addr:4;	
					
	unsigned char zerobit4:1;
	unsigned char zerobit5:1;
	unsigned char statusbit:1;
	unsigned char zerobit6:1;
	
	unsigned char primaryport:1;     
	unsigned char zerobit7:1;
	unsigned char sec_addr:6;      
	
	unsigned char reserved3:8;
	msg_type_enum msg_type:8;	
	unsigned long sender_ID;	

};

推荐答案

可能您忘记指定的位大小msg_size ,如果有效,可能会使数据对齐。请检查。



还有一件事:属于所谓的基本类型;它们的实际大小取决于平台: https://msdn.microsoft.com/en-us/library/cc953fe1 .aspx [ ^ ] 。



所以,在你需要精确位数的情况下,我会避免使用这些类型。出于这样的目的,C ++ 11(我明白,这不是关于VS 6.0)定义了以下内容: http: //en.cppreference.com/w/cpp/types/integer [ ^ ]。



如果你没有C ++ 11,你可以使用例如特定于平台的定义,例如Windows WORD等。



顺便说一下,如果你怀疑某些版本的C ++会出现某些版本的Visual Studio做错了什么,你总是可以比较它与VS 2013或VS 2015社区版免费提供。



-SA
Probably you forgot to specify the bit size for msg_size, and that may put data alignment if effect. Please check up.

One more thing: short and long are among so called "fundamental types"; their actual size depends on platform: https://msdn.microsoft.com/en-us/library/cc953fe1.aspx[^].

So, I would avoid using such types in the situations where you need exact bit size. For such purposes, C++11 (I do understand, this is not about VS 6.0) defines the following: http://en.cppreference.com/w/cpp/types/integer[^].

If you don't have C++11, you can use, for example, platform-specific definitions, such as Windows WORD, etc.

By the way, if you suspect some version of C++ coming with some version of Visual Studio does something wrong, you can always compare it with VS 2013 or VS 2015, Community Edition, which come free of charge.

—SA


默认情况下,Visual C / C ++编译器将在8字节边界处对齐结构成员,并在必要时插入填充字节。请参阅MSDN文章使用包装结构 [ ^ ]。



为避免这种情况,您可以使用 pack pragma [ ^ ]以指定1字节对齐。



使用位字段时更复杂。然后编译器将仅合并使用相同整数类型的那些字段。但是,这可以用于通过对所有位字段结构成员使用相同(最大)的整数类型来确保有效位置。因此,您可以将结构定义为:

By default the Visual C/C++ compiler will align structure members at 8 byte boundaries inserting padding bytes if necessary. See the MSDN article Working with Packing Structures[^].

To avoid this, you can use pack pragma[^] to specifiy 1 byte alignment.

When using bit fields it is even more complicated. Then the compiler will only merge those fields that use the same integral type. But this can be used to ensure valid positions by using the same (largest) integral type for all bit field struct members. So you may define your structure as:
#pragma pack(1)
struct message_header_struct
{
    unsigned msg_header3:8;
    unsigned msg_header2:8;
    unsigned msg_header1:8;
    unsigned msg_header0:8;
    unsigned reserved1:8;
    unsigned reserved2:8;
    unsigned msg_size:16;

    unsigned pri_addr:4;

    unsigned zerobit4:1;
    unsigned zerobit5:1;
    unsigned statusbit:1;
    unsigned zerobit6:1;

    unsigned primaryport:1;
    unsigned zerobit7:1;
    unsigned sec_addr:6;

    unsigned reserved3:8;
    unsigned msg_type:8;
    unsigned sender_ID:32;
};
#pragma pack()







多年来这种情况没有改变,应该适用于6.0以来的所有Visual版本。




This has not changed for many years and should apply to all Visual versions since 6.0.


这篇关于Visual Studio 6.0是否存在已知缺陷?见下面的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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