Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      memmove, memmove_s

      From cppreference.com
      <c‎ |string‎ |byte
       
       
       
       
      Defined in header<string.h>
      void* memmove(void* dest,constvoid* src,size_t count);
      (1)
      errno_t memmove_s(void* dest, rsize_t destsz,constvoid* src, rsize_t count);
      (2)(since C11)
      1) Copiescount characters from the object pointed to bysrc to the object pointed to bydest. Both objects are interpreted as arrays ofunsignedchar. The objects may overlap: copying takes place as if the characters were copied to a temporary character array and then the characters were copied from the array todest.
      The behavior is undefined if access occurs beyond the end of the dest array. The behavior is undefined if eitherdest orsrc is an invalid or null pointer.
      2) Same as(1), except when detecting the following errors at runtime, it zeroes out the entire destination range[dest, dest+ destsz) (if bothdest anddestsz are valid) and calls the currently installedconstraint handler function:
      • dest orsrc is a null pointer
      • destsz orcount is greater thanRSIZE_MAX
      • count is greater thandestsz (buffer overflow would occur)
      The behavior is undefined if the size of the character array pointed to bydest <count <=destsz; in other words, an erroneous value ofdestsz does not expose the impending buffer overflow.
      As with all bounds-checked functions,memmove_s is only guaranteed to be available if__STDC_LIB_EXT1__ is defined by the implementation and if the user defines__STDC_WANT_LIB_EXT1__ to the integer constant1 before including<string.h>.

      Contents

      [edit]Parameters

      dest - pointer to the object to copy to
      destsz - max number of bytes to modify in the destination (typically the size of the destination object)
      src - pointer to the object to copy from
      count - number of bytes to copy

      [edit]Return value

      1) Returns a copy ofdest
      2) Returns zero on success and non-zero value on error. Also on error, ifdest is not a null pointer anddestsz is valid, writesdestsz zero bytes in to the destination array.

      [edit]Notes

      memmove may be used to set theeffective type of an object obtained by an allocation function.

      Despite being specified "as if" a temporary buffer is used, actual implementations of this function do not incur the overhead or double copying or extra memory. A common approach (glibc and bsd libc) is to copy bytes forwards from the beginning of the buffer if the destination starts before the source, and backwards from the end otherwise, with a fall back to the more efficientmemcpy when there is no overlap at all.

      Wherestrict aliasing prohibits examining the same memory as values of two different types,memmove may be used to convert the values.

      [edit]Example

      Run this code
      #define __STDC_WANT_LIB_EXT1__ 1#include <inttypes.h>#include <stdint.h>#include <stdio.h>#include <stdlib.h>#include <string.h> int main(void){char str[]="1234567890";puts(str);    memmove(str+4, str+3,3);// copy from [4,5,6] to [5,6,7]puts(str); // setting effective type of allocated memory to be intint* p=malloc(3*sizeof(int));// allocated memory has no effective typeint arr[3]={1,2,3};    memmove(p, arr,3*sizeof(int));// allocated memory now has an effective type // reinterpreting datadouble d=0.1;// int64_t n = *(int64_t*)(&d); // strict aliasing violationint64_t n;    memmove(&n,&d,sizeof d);// OKprintf("%a is %"PRIx64" as an int64_t\n", d, n); #ifdef __STDC_LIB_EXT1__    set_constraint_handler_s(ignore_handler_s);char src[]="aaaaaaaaaa";char dst[]="xyxyxyxyxy";int r= memmove_s(dst,sizeof dst, src,5);printf("dst =\"%s\", r = %d\n", dst, r);    r= memmove_s(dst,5, src,10);// count is greater than destszprintf("dst =\"");for(size_t ndx=0; ndx<sizeof dst;++ndx){char c= dst[ndx];        c?printf("%c", c):printf("\\0");}printf("\", r = %d\n", r);#endif}

      Possible output:

      123456789012344568900x1.999999999999ap-4 is 3fb999999999999a as an int64_tdst = "aaaaayxyxy", r = 0dst = "\0\0\0\0\0yxyxy", r = 22

      [edit]References

      • C23 standard (ISO/IEC 9899:2024):
      • 7.24.2.2 The memmove function (p: TBD)
      • K.3.7.1.2 The memmove_s function (p: TBD)
      • C17 standard (ISO/IEC 9899:2018):
      • 7.24.2.2 The memmove function (p: 264)
      • K.3.7.1.2 The memmove_s function (p: 446)
      • C11 standard (ISO/IEC 9899:2011):
      • 7.24.2.2 The memmove function (p: 363)
      • K.3.7.1.2 The memmove_s function (p: 615)
      • C99 standard (ISO/IEC 9899:1999):
      • 7.21.2.2 The memmove function (p: 326)
      • C89/C90 standard (ISO/IEC 9899:1990):
      • 4.11.2.2 The memmove function

      [edit]See also

      copies one buffer to another
      (function)[edit]
      copies a certain amount of wide characters between two, possibly overlapping, arrays
      (function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=c/string/byte/memmove&oldid=178699"

      [8]ページ先頭

      ©2009-2025 Movatter.jp