|
写了如下程序,-
- #include "ace/OS.h"
- #include "ace/ACE.h"
- #include "ace/Message_Block.h"
- #include "ace/Log_Msg.h"
- #pragma pack(1)
- struct Head
- {
- int status;
- short seq;
- char name[20];
- };
- #pragma pack()
- int
- main(void)
- {
- ACE_Message_Block *mblk = new ACE_Message_Block (sizeof (Head) * 4);
- Head First_Head;
- First_Head.status = 0;
- First_Head.seq = 12456;
- ACE_OS::strcpy(First_Head.name, "Lien.C");
- mblk->copy((char*)&First_Head, sizeof (Head));
- ACE_DEBUG((LM_DEBUG, "Message Block Size = %d\n", mblk->size()));
- Head Second_Head;
- mblk->copy((char*)&First_Head, sizeof (Head));
- ACE_OS::memcpy(&Second_Head, mblk->rd_ptr(), sizeof Second_Head);
- Head Third_Head;
- mblk->rd_ptr(sizeof (Head) * 2);
- ACE_OS::strcpy(First_Head.name, "中华人民共和国");
- mblk->copy((char*)&First_Head, sizeof (Head));
- ACE_OS::memcpy(&Third_Head, mblk->rd_ptr(), sizeof Second_Head);
-
-
- ACE_DEBUG((LM_DEBUG, "Message Block Size = %d\n", mblk->space()));
-
- ACE_DEBUG((LM_DEBUG, "Second_Head Infomation: name - %s\n", Second_Head.name));
- ACE_DEBUG((LM_DEBUG, "Third_Head Infomation: name - %s\n", Third_Head.name));
- ACE_Message_Block *mblk_2 = mblk->clone();
- mblk_2->duplicate(mblk);
- ACE_DEBUG((LM_DEBUG, "Message Block Cloned Size = %d\n", mblk_2->size()));
- mblk_2->reset();
- mblk->cont(mblk_2);
-
- mblk->rd_ptr(sizeof (Head) * 3);
- Head Fourth_Head;
- ACE_OS::memcpy(&Fourth_Head, mblk->rd_ptr(), sizeof (Head));
- ACE_DEBUG((LM_DEBUG, "Third_Head Infomation: name - %s\n", Fourth_Head.name));
- mblk->release();
- mblk = 0;
- mblk_2->release();
- mblk_2 = 0;
-
-
- return 0;
- }
复制代码
运行结果:
Message Block Size = 104
Message Block Size = 26
Second_Head Infomation: name - Lien.C
Third_Head Infomation: name - 中华人民共和国
Message Block Cloned Size = 104
Third_Head Infomation: name - œß@(乱码)
Illegal instruction (不知道为什么会输出这个, 可能地址越界)
在做mblk->cont以后发现信息似乎没有被写进来. 请问是在mblk->clone()的时候出了问题, 还是我的读指针rd_ptr不对呢? |
|