1
我正在使用mqueue在線程之間進行通信,而且我在傳遞對象時遇到問題。使用mqqueue將數據讀入緩衝區時遇到問題
mq_send
和mq_receive
以char*
作爲對象的參數。
我以如下方式使用它們。
foo* foo = new foo();
foo->set_id(3);
mq_send(myQueue, (char*)foo, 1024, 1);
然後
char* buffer;
while(true)
{
ssize_t bytes_read;
bytes_read = mq_receive(myQueue, buffer, 1024, NULL);
foo* foo = (foo*) buffer;
foo->get_id(); //equals 3
//Send the object to another queue
mq_send(myOtherQueue, buffer, 1024, 1);
}
到目前爲止好。
的問題是在這裏
char* buffer;
while(true)
{
ssize_t bytes_read;
bytes_read = mq_receive(myOtherQueue, buffer, 1024, NULL);
foo* foo = (foo*) buffer;
foo->get_id(); //equals garbage 323234234
}
我第二次投緩衝區,我得到的垃圾。 我讀了關於static_cast
和dynamic_cast
,我無法找到問題所在。
出了什麼問題?
謝謝@SergeyA。我不明白你的第二點。 mq_receive設置緩衝變量 – Marc
@Marc,不,它將數據複製到提供的緩衝區。但是你的緩衝區沒有分配。 – SergeyA
基於長度(1024)的mq_receive沒有完成嗎? – Marc