| Functions | |||||||||||||||||||||||||||||||||||||||||
| Character manipulation | |||||||||||||||||||||||||||||||||||||||||
| Conversions to and from numeric formats | |||||||||||||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||||||||||||
| String manipulation | |||||||||||||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||||||||||||
| String examination | |||||||||||||||||||||||||||||||||||||||||
| Memory manipulation | |||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||
| Miscellaneous | |||||||||||||||||||||||||||||||||||||||||
(C11)(C11) | |||||||||||||||||||||||||||||||||||||||||
Defined in header <string.h> | ||
| (1) | ||
void* memcpy(void*dest,constvoid*src,size_t count); | (until C99) | |
void* memcpy(void*restrict dest,constvoid*restrict src,size_t count); | (since C99) | |
errno_t memcpy_s(void*restrict dest, rsize_t destsz, constvoid*restrict src, rsize_t count); | (2) | (since C11) |
count characters from the object pointed to bysrc to the object pointed to bydest. Both objects are interpreted as arrays ofunsignedchar.dest array. If the objects overlap (which is a violation of therestrict contract)(since C99), the behavior is undefined. The behavior is undefined if eitherdest orsrc is an invalid or null pointer.dest anddestsz are valid), as well as call the currently installedconstraint handler function:dest orsrc is a null pointerdestsz orcount is greater thanRSIZE_MAXcount is greater thandestsz (buffer overflow would occur)dest <count <=destsz; in other words, an erroneous value ofdestsz does not expose the impending buffer overflow.memcpy_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 |
| 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 |
destdest is not a null pointer anddestsz is valid, writesdestsz zero bytes in to the destination array.memcpy may be used to set theeffective type of an object obtained by an allocation function.
memcpy is the fastest library routine for memory-to-memory copy. It is usually more efficient thanstrcpy, which must scan the data it copies ormemmove, which must take precautions to handle overlapping inputs.
Several C compilers transform suitable memory-copying loops tomemcpy calls.
Wherestrict aliasing prohibits examining the same memory as values of two different types,memcpy may be used to convert the values.
#define __STDC_WANT_LIB_EXT1__ 1#include <stdio.h>#include <stdint.h>#include <inttypes.h>#include <string.h>#include <stdlib.h> int main(void){// simple usagechar source[]="once upon a midnight dreary...", dest[4]; memcpy(dest, source,sizeof dest);for(size_t n=0; n<sizeof dest;++n)putchar(dest[n]); // 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}; memcpy(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; memcpy(&n,&d,sizeof d);// OKprintf("\n%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= memcpy_s(dst,sizeof dst,src,5);printf("dst =\"%s\", r = %d\n", dst,r); r= memcpy_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:
once0x1.999999999999ap-4 is 3fb999999999999a as an int64_tdst = "aaaaayxyxy", r = 0dst = "\0\0\0\0\0yxyxy", r = 22
(C23) | copies one buffer to another, stopping after the specified delimiter (function)[edit] |
(C11) | moves one buffer to another (function)[edit] |
(C95)(C11) | copies a certain amount of wide characters between two non-overlapping arrays (function)[edit] |
C++ documentation formemcpy | |