This is struct node with 2 variables.
typedef struct{
int a;
int b;
}node;
Inserting the values into vector of the node type.
vector <node> nv;
node n;
n.a = 10;
n.b = 20;
nv.push_back(n);
n.a = 100;
n.b = 200;
nv.push_back(n);
Copying the vector to a void pointer.
void *ptr = malloc(4096);
memcpy((char*)ptr,&nv,sizeof(node)*2);
Reading the values from the void pointer
node *temp;
temp = (node*)malloc(sizeof(node));
int offset = 0;
i = 0;
while(i<n){
memcpy((node*)temp,(char*)ptr + offset, sizeof(node));
cout<<"a - "<<temp->a<<" b -"<<temp->b<<endl;
offset += sizeof(node);
i++;
}
I am printing the values of a and b. But they are incorrect and contains random numbers. I am sure that I am hitting the wrong memory location. But not sure where.