1
我有這樣的工會:多態性c和緩衝區
typedef union Message
{
message_base base;
message_with_parameters parameters;
reply_message reply;
buffer_t *buffer; // can't figure out what to put here
} message;
message_with_parameters
有message_base
作爲第一場和reply_message
有message_with_parameters
作爲所述輪流具有message_base
以作爲第一場的第一場。
所以基本上我可以訪問他們中的任何一個,我仍然可以獲得所需的所有數據,但是我從我的驅動程序獲取緩衝區,現在我想將其序列化爲消息。
我已經知道指向緩衝區的指針是錯誤的,因爲它不會與我的結構相關,但我不能擁有固定大小的緩衝區。
某處一路上我想這樣做:
m->buffer = buff->payload;
而且不管我有什麼樣的數據類型,它仍然會序列。
怎麼辦?
編輯:
這裏是我的結構:
typedef struct MessageBase
{
uint32_t u32DeviceID;
uint32_t u32CoreID;
uint16_t u16Class;
uint16_t u16CRC;
uint8_t u8OpCode;
void (*states [MAX_OPCODES]) (void *);
} message_base;
typedef struct MessageWithParameters
{
message_base base_class;
uint8_t u8Param1;
uint8_t u8Param2;
} message_with_parameters;
typedef message_with_parameters reply_message;
typedef union Message
{
message_base base;
message_with_parameters parameters;
reply_message reply;
} message;
我只得到垃圾數據的一部分。即使我輸入的值是正確的。 – 2010-08-16 00:12:24
很難知道你在做什麼,我只是猜測一下。 你有一個數據緩衝區是一個「消息」,所以你已經定義了一個消息聯盟(我有點懷疑,我不知道所有這些領域應該是一個聯盟?或者它是否應該是一個作爲工會的結構?)。因此,您的緩衝區中的數據,第一個字節將成爲消息庫中的第一個字節,並且它將成爲reply_message的第一個字節,並且它將成爲message_with參數的第一個字節。 – 2010-08-16 00:44:14
@凱斯尼古拉斯:查看我編輯的結構。 有效載荷的類型爲void *如果你想知道 – 2010-08-16 00:58:26