#include<iostream>
usingnamespace std;
class sample
{
public:
int value;
unsignedchar p;
};
class timer
{
public:
char arr[200];
};
int main()
{
sample* ptr=new sample;
ptr->value = 5;
ptr->p = 'c';
cout<<"address of ptr "<<&ptr<<endl;
cout<<endl;
/*
timer* time;
memset(&time,0,sizeof(timer));
memcpy(time->arr,ptr,sizeof(sample));
*/
timer time;
memset(&time,0,sizeof(timer));
memcpy(time.arr,ptr,sizeof(ptr));
return 0;
}
Program doesn't give segmentation fault but I am not sure whether the method I have used is right. Also, I assume the pointer location of ptr is stored in first 4 or 8 (for 64 bits) bytes of "arr".
If the method is right, then
1. How do I make sure the values are correctly copied? I mean how do I print out the actual values?
2. While retrieving data from the timer class, how I do get it back? Using same method, that is, find the pointer location first and then values or something else needs to be done?