memcpy - overlapping regions of memory

Post a reply


In an effort to prevent automatic submissions, we require that you complete the following challenge.
Smilies
:D :) ;) :( :o :shock: :? 8-) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :!: :?: :idea: :| :mrgreen: :geek: :ugeek: :arrow: :angel: :clap: :crazy: :eh: :lolno: :problem: :shh: :shifty: :sick: :silent: :think: :thumbup: :thumbdown: :salute: :wave: :wtf: :yawn: :facepalm: :bravo: :dance: :beard: :morebeard: :xmas: :HeHe: :trollface: :cookie: :rainbow: :monkeysee: :monkeysay: :happybday: :headwall: :offtopic: :superhappy: :terms: :beer:
View more smilies

BBCode is ON
[img] is OFF
[flash] is OFF
[url] is ON
Smilies are ON

Topic review
   

Expand view Topic review: memcpy - overlapping regions of memory

memcpy - overlapping regions of memory

Post by trismarck » 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

Top