Problem with serializing & deserializing char*

44 views
Skip to first unread message

Rex Stefan

unread,
Aug 16, 2016, 1:21:39 PM8/16/16
to ns-3-users
Dear Ns-3 users,

I am trying to serialize and deserialize the char* type by the following way but getting segmentation fault at the deserialize function. Could someone please help me out with it. I have highlighted the line where I am getting error.

void
GoosePduHeader::Serialize (Buffer::Iterator start) const
{

uint16_t x;
Buffer::Iterator i = start;
for (x=0;x<sizeof(this->msg->gocbRef);x++)
    {
    i.WriteU8 (this->msg->gocbRef[x]);
    }


uint32_t
GoosePduHeader::Deserialize (Buffer::Iterator start)
{

uint16_t x;
Buffer::Iterator i = start;
{
for (x=0;x<sizeof(this->msg->gocbRef);x++)
{
this->msg->gocbRef[x]=i.ReadU8();
}
}


Error message:
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff5de52b5 in ns3::GoosePduHeader::Deserialize (this=0x65b150, 
    start=...) at ../src/applications/model/apdu.cc:362
362 this->msg->gocbRef[x]=i.ReadU8();


Thanks a lot in advance!

Regards,
Rex

Konstantinos

unread,
Aug 16, 2016, 5:48:42 PM8/16/16
to ns-3-users
Hi Rex,

The output is clear where the problem comes from.

for (x=0;x<sizeof(this->msg->gocbRef);x++)
{
 
this->msg->gocbRef[x]=i.ReadU8();
}

The reason is that when you try to read the packet, 'this->msg->gocbRef' is not initialized because it is the 'received' message.
So, when you try to find its size, it is not there.

The solution, which is also how real systems work and has been described several times here, is to add another value in front to specify how much data you write/read.

Regards,
K

Rex Stefan

unread,
Aug 24, 2016, 8:15:56 PM8/24/16
to ns-3-users
Dear Konstantinos,

I am extremely sorry for getting back to you very late since I was busy with my other part of the project. I have assigned the sizeof(this->msg->gocbRef) to a variable called m_gocbRef and used it in the for loop as mentioned below. When I checked the value of m_gocbRef using gdb debugger, I get the exact size of this->msg->gocbRef. So I am just confused how to get rid of this. Could you please suggest me an approach to fix the same. Thanks a lot in advance.


uint32_t
GoosePduHeader::Deserialize (Buffer::Iterator start)
{

uint16_t x;
Buffer::Iterator i = start;
{
uint32_t m_gocbRef = sizeof(this->msg->gocbRef);
//this->msg->gocbRef = new char[m_gocbRef];
for (x=0;x<m_gocbRef;x++)
{
this->msg->gocbRef[x]=i.ReadU8();
}
}
 }


Regards,
Rex Stefan Edberg

pdbarnes

unread,
Aug 25, 2016, 1:36:12 AM8/25/16
to ns-3-users
I can help weigh the first error. It should be

Simulator::Schedule (Seconds (m_totalTime), &MeshTest::Report, this, std::cout);

However, the second error will still occur. Are you sure &MeshTest::Report() can write directly to std::cout? Check the usage in the examples.

Peter

Reply all
Reply to author
Forward
0 new messages