How to serialize "char*" which is a pointer to a byte array to my header?

142 views
Skip to first unread message

Ali Marandi

unread,
May 16, 2011, 3:42:16 AM5/16/11
to ns-3-users
Hello,
I want to serialize and deserialize "char*" which is a pointer to a
byte array to my header (I mean it does not point to a null terminated
string and it is just a pointer to an array of characters). It seems
that we cannot serialize a pointer to the header. So would you please
help me how can I serialize "char*" to my header?

Best Regards,
Ali.

Guillaume Rémy

unread,
May 16, 2011, 4:47:07 AM5/16/11
to ns-3-...@googlegroups.com
Hi,

Of course you can serialize and deserialize a char*, but you have to know your array size. Assuming you char* is named m_str, its length is named m_lg, and that your header contains only this pointer, the following code will serialize and deserialize it:

void
YourHeader::Serialize (Buffer::Iterator start) const
{
  Buffer::Iterator it = start;
  for (uint i=0;i<m_lg;i++)
  {
    it.WriteU8 (m_str[i]);
  }
}

uint32_t
YourHeader::Deserialize (Buffer::Iterator start)
{
  Buffer::Iterator it = start;
  if (m_str) delete m_str;
  m_str = new char[m_lg];
  for  (uint i=0;i<m_lg;i++)
  {
    m_str[i] = it.ReadU8 ();
  }
  return GetSerializedSize ();
}

Regards,
Guillaume

Lalith Suresh

unread,
May 16, 2011, 5:16:02 AM5/16/11
to ns-3-...@googlegroups.com
Hello Ali,

In real world networks, it makes no sense to serialise a pointer into a header because the address pointed to by the pointer has no meaning in the context of another node in the network. I would recommend serialising all the bytes that the char * is pointing to into the header.

If you really insist on sending a char * across, serialise the address as a 32 or 64 bit unsigned integer depending on your machine's architecture.

 
Best Regards,
Ali.

--
You received this message because you are subscribed to the Google Groups "ns-3-users" group.
To post to this group, send email to ns-3-...@googlegroups.com.
To unsubscribe from this group, send email to ns-3-users+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/ns-3-users?hl=en.




--
Lalith Suresh
Department of Computer Science and Engineering
Instituto Superior Técnico
www.lalith.in

Guillaume Rémy

unread,
May 16, 2011, 7:59:30 AM5/16/11
to ns-3-...@googlegroups.com
Hi lalith,

It has indeed no sense to put just the pointer in the header, it is the reason why I assumed he want to put all the chars pointed by its pointer in the header (he precised it is an array of char). Then, the code I gave doesn't serialize the address contained in the pointer, but the objects pointed by the pointer.

Regards,
Guillaume

Ali Marandi

unread,
May 16, 2011, 8:26:59 AM5/16/11
to ns-3-users
Thank you very much my friends :-).
Reply all
Reply to author
Forward
0 new messages