memcpy() and uint8_t

Jan 18, 2010 at 9:46am
Hello folks..

In the following code, I m trying to copy struct element to array...


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include<iostream>
#include<stdint.h>
#include<string>

using namespace 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?


Thank You
Jan 18, 2010 at 12:40pm
hi Katty,

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.

I have modified the program, hope it helps :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include<iostream>
#include<stdlib.h>
#include<stdint.h>
#include<string>

using namespace std;
struct test //total 15 bytes
{
	struct header
	{
		int a;
		int b;
	};
	uint8_t c;//1 byte typedef unsigned char     uint8_t;
	uint8_t d;
	uint8_t e;
	uint32_t f;//4 byte typedef unsigned int      uint32_t;
};
struct test2
{
	uint8_t n;
	uint8_t m;
	uint8_t p;
	
};


int main()
{
	test headd;
	test2 data;
    uint8_t* d;
	int x, y,i;
	uint8_t c;
	x = sizeof(headd);
	y= sizeof(test::header);
    i= sizeof(data);
	cout<<x<<endl;;
	cout<<y<<endl;

	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,sizeof(headd)+sizeof(test::header)); 
	memcpy(d+sizeof(headd)+sizeof(test::header),&data, sizeof(data));
	cout<<"struct test.c="<<*d;
	cout<<"struct test.d="<<*(d+1);
	return 0;
}
Topic archived. No new replies allowed.