|
| 1 | +--- |
| 2 | +title: memcpy, memcpy_s |
| 3 | +cppdoc: |
| 4 | + keys: ["c.library.memcpy", "c.library.memcpy_s"] |
| 5 | +--- |
| 6 | + |
| 7 | +import Behavior from "@components/Behavior.astro"; |
| 8 | +import { CHeader } from "@components/header"; |
| 9 | +import { Decl, DeclDoc } from "@components/decl-doc"; |
| 10 | +import { Desc, DescList } from "@components/desc"; |
| 11 | +import Missing from "@components/Missing.astro"; |
| 12 | +import { ParamDoc, ParamDocList } from "@components/param-doc"; |
| 13 | +import { Revision, RevisionBlock } from "@components/revision"; |
| 14 | + |
| 15 | +Defined in header <CHeader name="string" />. |
| 16 | + |
| 17 | +<DeclDoc id={1}> |
| 18 | + <Decl slot="decl"> |
| 19 | + <RevisionBlock until="C99" noborder> |
| 20 | + ```c |
| 21 | + void* memcpy(void *dest, const void *src, size_t count); |
| 22 | + ``` |
| 23 | + </RevisionBlock> |
| 24 | + </Decl> |
| 25 | + <Decl slot="decl"> |
| 26 | + <RevisionBlock since="C11" noborder> |
| 27 | + ```c |
| 28 | + void* memcpy(void *restrict dest, const void *restrict src, |
| 29 | + size_t count); |
| 30 | + ``` |
| 31 | + </RevisionBlock> |
| 32 | + </Decl> |
| 33 | + |
| 34 | + Copies `count` characters from the object pointed to by `src` to the object pointed to by `dest`. Both objects are interpreted as arrays of `unsigned char`. |
| 35 | + |
| 36 | + The behavior is <Behavior kind="undef">undefined</Behavior> if access occurs beyond the end of the dest array. If the objects overlap <Revision since="C99">(which is a violation of the `restrict` contract)</Revision>, the behavior is <Behavior kind="undef">undefined</Behavior>. The behavior is <Behavior kind="undef">undefined</Behavior> if either dest or src is an invalid or null pointer. |
| 37 | +</DeclDoc> |
| 38 | + |
| 39 | +<DeclDoc id={2}> |
| 40 | + <Decl slot="decl"> |
| 41 | + <RevisionBlock since="C11" noborder> |
| 42 | + ```c |
| 43 | + errno_t memcpy_s(void *restrict dest, rsize_t destsz, |
| 44 | + const void *restrict src, rsize_t count); |
| 45 | + ``` |
| 46 | + </RevisionBlock> |
| 47 | + </Decl> |
| 48 | + |
| 49 | + Same as #1, except that the following errors are detected at runtime and cause the entire destination range `[dest, dest+destsz)` to be zeroed out (if both `dest` and `destsz` are valid), as well as call the currently installed <Missing>constraint handler</Missing> function: |
| 50 | + |
| 51 | + - `dest` or `src` is a null pointer |
| 52 | + - `destsz` or `count` is greater than `RSIZE_MAX` |
| 53 | + - `count` is greater than `destsz` (buffer overflow would occur) |
| 54 | + - the source and the destination objects overlap |
| 55 | + |
| 56 | + The behavior is <Behavior kind="undef">undefined</Behavior> if the size of the character array pointed to by `dest` < `count` \<= `destsz`; in other words, an erroneous value of `destsz` does not expose the impending buffer overflow. |
| 57 | + |
| 58 | + As with all bounds-checked functions, `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 constant `1` before including <CHeader name="string" />. |
| 59 | +</DeclDoc> |
| 60 | + |
| 61 | +## Parameters |
| 62 | + |
| 63 | +<ParamDocList> |
| 64 | + <ParamDoc name="dest"> |
| 65 | + pointer to the object to copy to |
| 66 | + </ParamDoc> |
| 67 | + <ParamDoc name="destsz"> |
| 68 | + max number of bytes to modify in the destination (typically the size of the destination object) |
| 69 | + </ParamDoc> |
| 70 | + <ParamDoc name="src"> |
| 71 | + pointer to the object to copy from |
| 72 | + </ParamDoc> |
| 73 | + <ParamDoc name="count"> |
| 74 | + number of bytes to copy |
| 75 | + </ParamDoc> |
| 76 | +</ParamDocList> |
| 77 | + |
| 78 | +## Return value |
| 79 | + |
| 80 | +1. Returns a copy of `dest` |
| 81 | +2. Returns zero on success and non-zero value on error. Also on error, if `dest` is not a null pointer and `destsz` is valid, writes `destsz` zero bytes in to the destination array. |
| 82 | + |
| 83 | +## Notes |
| 84 | + |
| 85 | +`memcpy` may be used to set the <Missing>effective type</Missing> of an object obtained by an allocation function. |
| 86 | + |
| 87 | +`memcpy` is the fastest library routine for memory-to-memory copy. It is usually more efficient than <Missing>`strcpy`</Missing>, which must scan the data it copies or <Missing>`memmove`</Missing>, which must take precautions to handle overlapping inputs. |
| 88 | + |
| 89 | +Several C compilers transform suitable memory-copying loops to `memcpy` calls. |
| 90 | + |
| 91 | +Where <Missing>strict aliasing</Missing> prohibits examining the same memory as values of two different types, `memcpy` may be used to convert the values. |
| 92 | + |
| 93 | +## Example |
| 94 | + |
| 95 | +```c |
| 96 | +#define __STDC_WANT_LIB_EXT1__ 1 |
| 97 | +#include <stdio.h> |
| 98 | +#include <stdint.h> |
| 99 | +#include <inttypes.h> |
| 100 | +#include <string.h> |
| 101 | +#include <stdlib.h> |
| 102 | + |
| 103 | +int main(void) { |
| 104 | + // simple usage |
| 105 | + char source[] = "once upon a midnight dreary..."; |
| 106 | + char dest[4]; |
| 107 | + memcpy(dest, source, sizeof(dest)); |
| 108 | + for (size_t n = 0; n < sizeof(dest); ++n) |
| 109 | + putchar(dest[n]); |
| 110 | + |
| 111 | + // setting effective type of allocated memory to be int |
| 112 | + int *p = malloc(3 * sizeof(int)); // allocated memory has no effective type |
| 113 | + int arr[3] = {1, 2, 3}; |
| 114 | + memcpy(p, arr, 3 * sizeof(int)); // allocated memory now has an effective type |
| 115 | + |
| 116 | + // reinterpreting data |
| 117 | + double d = 0.1; |
| 118 | + // int64_t n = *(int64_t *)(&d); // strict aliasing violation |
| 119 | + int64_t n; |
| 120 | + memcpy(&n, &d, sizeof d); // OK |
| 121 | + printf("\n%a is %" PRIx64 " as an int64_t\n", d, n); |
| 122 | + |
| 123 | +#ifdef __STDC_LIB_EXT1__ |
| 124 | + set_constraint_handler_s(ignore_handler_s); |
| 125 | + char src[] = "aaaaaaaaaa"; |
| 126 | + char dst[] = "xyxyxyxyxy"; |
| 127 | + int r = memcpy_s(dst, sizeof(dst), src, 5); |
| 128 | + printf("dst = \"%s\", r = %d\n", dst,r); |
| 129 | + r = memcpy_s(dst, 5, src, 10); // count is greater than destsz |
| 130 | + printf("dst = \""); |
| 131 | + for (size_t ndx = 0; ndx<sizeof(dst); ++ndx) { |
| 132 | + char c = dst[ndx]; |
| 133 | + c ? printf("%c", c) : printf("\\0"); |
| 134 | + } |
| 135 | + printf("\", r = %d\n", r); |
| 136 | +#endif |
| 137 | +} |
| 138 | +``` |
| 139 | +
|
| 140 | +Possible output: |
| 141 | +
|
| 142 | +``` |
| 143 | +once |
| 144 | +0x1.999999999999ap-4 is 3fb999999999999a as an int64_t |
| 145 | +dst = "aaaaayxyxy", r = 0 |
| 146 | +dst = "\0\0\0\0\0yxyxy", r = 22 |
| 147 | +``` |
| 148 | +
|
| 149 | +## References |
| 150 | +
|
| 151 | +- C11 standard (ISO/IEC 9899:2011): |
| 152 | + - 7.24.2.1 The memcpy function (p: 362) |
| 153 | + - K.3.7.1.1 The memcpy_s function (p: 614) |
| 154 | +- C99 standard (ISO/IEC 9899:1999): |
| 155 | + - 7.21.2.1 The memcpy function (p: 325) |
| 156 | +- C89/C90 standard (ISO/IEC 9899:1990): |
| 157 | + - 4.11.2.1 The memcpy function |
0 commit comments