#include<iostream>
#include<stdint.h>
#include<string>
usingnamespace std;
struct test
{
struct header
{
int a;
int b;
};
uint8_t c;
uint8_t d;
uint8_t e;
uint32_t f;
};
struct test2
{
uint8_t n;
uint8_t m;
uint8_t p;
};
int main()
{
test headd;
test2 data;
uint8_t* d;
int x, y,i;
x= sizeof(test);
y= sizeof(test::header);
i= sizeof(data);
x=x+y+i;
d = new uint8_t[x+10];
cout<<"x ="<<x<<endl;
/** Assign **/
headd.c = 4;
headd.d = 5;
data.n=8;
data.m=7;
data.p=9;
/** copy 2 elements of struct 'headd' and struct 'data' on to memory block pointed by 'd' **/
memcpy(d,&headd.c,8);
cout<<"d="<<*d<<endl;
cout<<"d2="<<*(d+1)<<endl;
memcpy(d+8,&data, 8);
cout<<"d="<<*d<<endl;
cout<<"d2="<<*(d+2)<<endl;
return 0;
}
OUTPUT:
x =19
d=
d2=
d=
d2=
I ve replaced uint8_t and uint32_t by datatype 'int' and it works well... i can display contents of 'd'
but not for uint8_t and uint32_t .
Please suggest me how can i check whether data successfully copied to destination?
You have declared the variable as uint8_t which is unsigned char and takes 1 byte of memory and uint32_t is unsigned int and takes 4 bytes of memory.
So when you assign as headd.c = 4, it cannot accomodate the data with in, correct?
Moreover you have declared structure inside a structure so you while copying the data you must take into account the total size of the structure i.e size of outer struct + size of inner struct.