memcpy と memmove

事の始まり



(Mac OS)(Linux) Segmentation Fault 

 



使
( memcpy  memcpy 12)

 Dash memcpy 

If the objects overlap, the behavior is undefined.


 undefined 
()


memcpy 1dest > src 

 memmove 使
memmove  src  dest src  dest ()

memcpy 

Mac OS  memcpy


http://www.opensource.apple.com/source/xnu/xnu-2050.18.24/libsyscall/wrappers/memcpy.c

memcpy  dest < src 
dest < src 
if ((unsigned long)dst < (unsigned long)src) {
    /*
     * Copy forward.
     */

     // 省略
} else {
    /*
     * Copy backwards.  Otherwise essentially the same.
     * Alignment works as before, except that it takes
     * (t&wmask) bytes to align, not wsize-(t&wmask).
     */

     // 省略
}

memmove  memcpy 
 Mac OS 
__private_extern__ void *
memmove(void *s1, const void *s2, size_tn)
{
    return memcpy(s1, s2, n);
}

Linux  memcpy


https://github.com/torvalds/linux/blob/master/arch/alpha/lib/memcpy.c
void * memcpy(void * dest, const void *src, size_tn)
{
    if (!(((unsigned long) dest ^ (unsigned long) src) & 7)) {
        __memcpy_aligned_up ((unsigned long) dest, (unsigned long) src, n);
        return dest;
    }
    __memcpy_unaligned_up ((unsigned long) dest, (unsigned long) src, n);
    return dest;
}

dest  src 3
使 __memcpy_aligned_up __memcpy_unaligned_up  __memcpy_aligned_dn__memcpy_unaligned_dn 使
up down  forward backward 



memcpy  memmove 使

Mac OS memcpy  ( memmove 使)


Cmemcpy 使 undefined 
 memmove 



 memmove 


C使

 Dash 

memcpy 使

2015-01-13 



id:mnogu 

alphaLinux kernel?
glibcmemcpy.cOverlap is NOT handled correctly.
https://sourceware.org/git/?p=glibc.git;a=blob;f=string/memcpy.c;hb=HEAD

memcpy 調