zhanglm 发表于 2008-3-24 09:35:55

ACE_InputCDR的数据对齐问题

测试程序如下
#include <ace/ACE.h>
#include <ace/CDR_Stream.h>
#include <ace/Message_Block.h>
#include <ace/Log_Msg.h>
#include <ace/OS.h>
#pragma pack(1)
typedef struct tagMsgHeader
{
unsigned int startFlag;
unsigned short bodyLength;
unsigned int messageSequence;
unsigned char messageCmdCode;
}MsgHeader;
#pragma pack()
int ACE_TMAIN(int, ACE_TCHAR *[])
{
MsgHeader h;
memset(&h, 0, sizeof(MsgHeader));

h.startFlag = 0xdbdbdbdb;
h.bodyLength = 0xeeff;
h.messageSequence = 0xadcdef;
h.messageCmdCode = 0x08;

int hlen = sizeof(h);
int slen = sizeof(MsgHeader);

ACE_InputCDR cdr((const char *)&h, sizeof(h));
ACE_CDR::ULong startFlag;
ACE_CDR::Short bodyLength;
ACE_CDR::ULong messageSequence;
ACE_CDR::Char messageCmdCode;
cdr >> startFlag;
cdr >> bodyLength;
cdr >> messageSequence;
cdr >> messageCmdCode;

MsgHeader h2;
h2.startFlag = startFlag;
h2.bodyLength = bodyLength;
h2.messageCmdCode = messageCmdCode;
h2.messageSequence = messageSequence;
return 0;
}

调试时
cdr >> messageSequence;
出现问题,得不到正确的结果0xadcdef,而是无效的0xcccccccc,跟踪进去,发电 good_bit()返回值为false,而
h2.messageSequence = messageSequence;
却得到了messageCmdCode的首字节0xef,这是怎么回事?是不是在字节对齐方面ACE_InputCdr需要做特殊的处理?

yyi.mailer 发表于 2008-3-24 11:19:05

:),在编译ACE时在config.h中加上‘#define ACE_LACKS_CDR_ALIGNMENT’
页: [1]
查看完整版本: ACE_InputCDR的数据对齐问题