memcpy - overlapping regions of memory

Discuss other programming languages besides AutoHotkey
User avatar
trismarck
Posts: 506
Joined: 30 Sep 2013, 01:48
Location: Poland

memcpy - overlapping regions of memory

29 Nov 2014, 21:32

MS documentation on memcpy advises not to use memcpy when memory regions overlap. But despite that, even if memory regions do overlap, memcpy seems to work exactly as memmove. Swapping src and dst pointers doesn't seem to change the result of memcpy in comparison to memmove.
Is it normal that despite what the documentation says, MS version of memcpy has protection against overlapping memory buffers? or I'm just misunderstanding something/there are errors in the code below.

Code: Select all

memcpy(dest, src, size) {
	return DllCall("MSVCRT.dll\memcpy", "Ptr", dest, "Ptr", src, "UInt", size)
}
memmove(dest, src, size) {
	return DllCall("MSVCRT.dll\memmove", "Ptr", dest, "Ptr", src, "UInt", size)
}


VarSetCapacity(var, 5000)
var := "012345678901234567890123456789012345678901234567890123456789"
offset_src  := 20
offset_dest := 31
	; even swapping the offsets doesn't make memcpy() produce a different
	; result than memmove()
	; 
size := 30

buf1 := var
buf2 := var
buf1_dest := &buf1 + offset_dest
buf2_dest := &buf2 + offset_dest
buf1_src  := &buf1 + offset_src
buf2_src  := &buf2 + offset_src
memcpy( buf1_dest, buf1_src, size)
memmove(buf2_dest, buf2_src, size)
if(buf1 != buf2)
	msgbox %   buf1
		. "`n" buf2

Return to “Other Programming Languages”

Who is online

Users browsing this forum: No registered users and 4 guests