I have a char array that should be converted to a struct. I can't figure out how to fix memory error that my code is causing. Console is printing the expected result, however the program crashes at the end of main().
#include <iostream>
#include <map>
using namespace std;
struct lsa
{
int id;
int seqNum;
map<int, int> neighbors;
lsa()
{
id = 1;
seqNum = 1;
}
friend ostream& operator<<(ostream& os, const lsa& a)
{
os << "id: " << a.id << ", seqNum: " << a.seqNum << ", # of neighbors: " << a.neighbors.size() << endl;
return os;
}
};
int main()
{
lsa msg;
msg.id = 4;
msg.seqNum = 5;
msg.neighbors.insert(make_pair(2, 2));
msg.neighbors.insert(make_pair(3, 3));
const int bufsize = 1024;
char data[bufsize];
memset(data, 0, bufsize);
// turn LSA into bytes, that will be stored in "data"
memcpy(data, &msg, sizeof(msg));
// move data bytes to "out" LSA
lsa out;
memcpy(&out, &data, sizeof(data));
cout << out << endl;
}