0

I am developing an app to send and receive packets.

I am working with the std::vector class (first I started making the packets in arrays and I am open to change the method if you suggest one better).

I found this smart function to add any const char* to a vector object. But I would like to do another one to add them between {.. , ..} like I can do in C#. Here an example:

vector<char*> packet;

char dest[] = { 0x10, 0x10, 0x0, 0x61 };
AppendLiteral(packet, dest); //Ok

/*Or directly:*/ AppendLiteral(packet, { 10, 10, 0, 20 }); //But this does not work

Is it possible in c++? I know in C# you can do something like this.

Edit:

A possible solution is:

vector<uint8> telegram;

vector<uint8> src = { 0x10, 0x10, 0x0, 0x20 };
vector<uint8> dst = { 0x10, 0x10, 0x0, 0x60 };

telegram.insert(telegram.end(), dst.begin(), dst.end());
telegram.insert(telegram.end(), src.begin(), src.end());

With a little function to write less:

template <typename T>
void Append(vector<T> *data, vector<T> *data2)
{
    data->insert(data->end(), data2->begin(), data2->end());
}
Community
  • 1
  • 1
ars1614
  • 69
  • 1
  • 11

2 Answers2

3

Your vector<char*> just contains pointers to variables of type char. It does not contain any data. This is dangerous, because said pointer can become dangling if the objects pointed to go out of scope.

Better use a data type as element type for your vector. If you want to store strings, use std::vector<std::string>. If you want to hold objects like char[4], use std::vetor<std::array<char,4>>. If you want to hold arrays of bytes of arbitrary length, use std::vetor<std::vector<char>>.

int main()
{
  using packet=std::array<char,4>; // also works with packet=std::string
                                   //              or packet=std::vector<char>

  std::vector<packet> packets;

  packets.push_back({0x10, 0x10, 0x0, 0x61});
  packets.push_back({10, 10, 0, 20});

  packet p={0x2,0x4,0xa,0xb};
  packets.push_back(p);
}
Walter
  • 44,150
  • 20
  • 113
  • 196
0

Of course.

std::vector<std::array<char, 4>> packet;

Try this one.

iDingDong
  • 447
  • 2
  • 11
  • I am not used to code a lot in c++ so I copied pasted the code in my program and I have got an ambiguous template parameter error with T. I am adding hexadecimal values so I don't know if char type is the optimal (in C# I use byte arrays) – ars1614 Nov 02 '15 at 12:31
  • What I want is if I have this arrays (telegram header) [01,00,01,01,00] I would like to add this other two [10,01] for example, I do not know if you know what I mean. – ars1614 Nov 02 '15 at 12:35
  • The packet does not only have 4 fields, I would like to add strings as long as I want each time. For example at the beginning I did it with arrays: char src[4] {0x10, 0x10, 0x0, 0x20}; char dst[4] {0x10, 0x10, 0x0, 0x60}; So later I putted them in a char[8] array. Next for example I added a char[64] array with more data. – ars1614 Nov 02 '15 at 12:47
  • So what you need is actually std::string, or a std::vector as the element type. – iDingDong Nov 02 '15 at 12:53