[ndnSIM] How to add additional std::vector<std::string> type field in Interest packet

Junxiao Shi shijunxiao at email.arizona.edu
Tue May 22 06:07:56 PDT 2018

Hi Mudasir

> //for MyVector
> std::vector<std::string> myTempVector = this->getMyVector();
>     totalLength += encoder.prependByteArray(
>             reinterpret_cast<uint8_t*>(&myTempVector),
> sizeof(myTempVector));
>     totalLength += encoder.prependVarNumber(sizeof(myTempVector));
>     totalLength += encoder.prependVarNumber(tlv::MyVector);

> //for MyVector
> val = m_wire.find(tlv::MyVector);
> if (val == m_wire.elements_end()) {
>     BOOST_THROW_EXCEPTION(Error("MyVector element is missing"));
> }
> std::vector<std::string> myTemVector;
> if (val->value_size() != sizeof(myTemVector)) {
>     BOOST_THROW_EXCEPTION(Error("myTemVector element is malformed"));
> }
> std::memcpy(&myTemVector, val->value(), sizeof(myTemVector));
> m_MyVector = myTemVector;
> *Problem:* I'm following these 1-to-5 steps in order to add an Integer
> and a std::vector<std::string> fields in Interest packet but it gives an
> error on *wireDecode* while decoding MyVector field and simulation stops
> and terminates. But it does not give error on MyInteger field. Am I doing
> it right? Am I missing something? Please help me solving this I'm stuck on
> this point from many days.

You did not serialize <https://en.wikipedia.org/wiki/Serialization> the
std::vector<std::string>, but merely copied the header structure of
std::vector<std::string>. Thus, you triggered undefined behavior in STL

Learn to write the following two programs in C++:

   1. Given a std::vector<std::string>, write it to a file.
   2. Read the above file into a std::vector<std::string>, and print it on

After you wrote these programs, you can use the same technique to write to
and read from TLV structures.

Yours, Junxiao
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.lists.cs.ucla.edu/pipermail/ndnsim/attachments/20180522/e0f3bc05/attachment.html>

More information about the ndnSIM mailing list