Old answer
You want memmove() . Full description:
Memmove () must copy n bytes from the object pointed to by s2 to the object pointed to by s1. Copying occurs as if n bytes from the object pointed to by s2 are first copied to a temporary array of n bytes that does not overlap the objects pointed to by s1 and s2, and then n bytes from the temporary array are copied to the object, onto which indicates s1.
On the memcpy() page:
If copying occurs between overlapping objects, the behavior is undefined.
In any case, you should use memmove() . This is because the result of using memcpy() not reliable.
Matching bits for the actual question
You are requesting memcpy(ptr + 4, ptr, 8); which says it copies 8 bytes from ptr and puts them in ptr+4 . ptr is 123400000000, the first 8 bytes are 1234000, so it does this:
Original : 123400000000 Writes : 12340000 Result : 123412340000
You need to call:
memcpy(ptr+4, ptr, 4); memcpy(ptr+8, ptr, 4);
To achieve what you need. Or implement the equivalent. This should do it, but it has not been tested and is equivalent to memcpy; you need to either add an extra temporary buffer, or use two non-overlapping memory areas.
void memexpand(void* result, const void* start, const uint64_t cycle, const uint64_t limit) { uint64_t count = 0; uint8_t* source = start; uint8_t* dest = result; while ( count < limit ) { *dest = *source; dest++; count++; if ( count % cycle == 0 ) { source = start; } else { source++; } } }
user257111
source share