struct item
{
int a;
};
int main()
{
item *a = (item *)malloc(sizeof(item));
item *b = (item *)malloc(sizeof(item));
short *c = (short *)b;
c += 3;
memcpy(a, c, sizeof(int));
free(a);
free(b);
return 0;
}
Why does valgrind
echo "Invalid read of size 2"? I think it should be size 4.
Example message from Valgrind:
==19134== Invalid read of size 2
==19134== at 0x4C2F7E0: memcpy@@GLIBC_2.14 (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==19134== by 0x400625: main (main.cpp:19)
==19134== Address 0x51fd096 is 2 bytes after a block of size 4 alloc'd
==19134== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==19134== by 0x4005FC: main (main.cpp:16)