I found something strange about the memcpy() and memset() functions in MSVC2017 and I cannot explain it. Specifically, the 'destination' gets indexed not by single bytes but by the whole size of a structure ('size' argument).
So I have a struct:
typedef struct _S
{
int x;
int y;
} S;
And the code goes like follows:
S* array = (S*)malloc(sizeof(S) * 10); /* Ok. Allocates enough space for 10 structures. */
S s; /* some new structure instance */
/* !!! here is the problem.
* sizeof(S) will return 8
* 8*1 = 8
* now the starting address will be: array+8
* so I'm expecting my structure 's' to be copied to
* the second ''element'' of 'array' (index 1)
* BUT in reality it will be copied to the 7th index!
*/
memcpy(array + (sizeof(S) * 1), &s, sizeof(S));
/* After some tests I found out how to access 'properly' the
* 'array':
*/
memcpy(array + 1, &s, sizeof(S); /* this will leave the first struct
in the 'array' unchanged and copy 's's contents to the second
element */
Same for memset(). So far I thought the indexing should be done manually, providing the size of the copied object as well, but no?
memcpy(destination + (size * offset), source + (size * offset), size)
Am I doing something wrong?