I would like to copy 2 ints, 2 shorts and 1 char one after the other.
This is what I did:
int32_t a=1;
int32_t b=2;
int16_t c=3;
int16_t d=4;
int8_t e=5;
char*buf=new char[104];
memcpy(buf, &a, 32);
memcpy(buf + 32, &b, 32);
memcpy(buf + 64, &c, 16);
memcpy(buf + 80, &d, 16);
memcpy(buf + 96, &e, 8);
Is this correct ? My debugger says that the third line affects the second, but maybe I’m just misusing my debugger (more specifically, it says that the value of *((int32_t*)(buf+32)) changed between the second and third memcpy).
Thanks.
You’ve conflated bits and bytes and are overreading and overwriting large sections of memory!
So, your buffer is about 8 times larger than it needs to be, and you’re copying 4 times as much data as needed every time.