diff options
Diffstat (limited to 'devdocs/c/string%2Fbyte%2Fmemcpy.html')
| -rw-r--r-- | devdocs/c/string%2Fbyte%2Fmemcpy.html | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/devdocs/c/string%2Fbyte%2Fmemcpy.html b/devdocs/c/string%2Fbyte%2Fmemcpy.html new file mode 100644 index 00000000..d2483fba --- /dev/null +++ b/devdocs/c/string%2Fbyte%2Fmemcpy.html @@ -0,0 +1,94 @@ + <h1 id="firstHeading" class="firstHeading">memcpy, memcpy_s</h1> <table class="t-dcl-begin"> <tr class="t-dsc-header"> <th> Defined in header <code><string.h></code> </th> <th> </th> <th> </th> </tr> <tr class="t-dcl-rev-aux"> <td></td> <td rowspan="3">(1)</td> <td></td> </tr> <tr class="t-dcl t-until-c99"> <td> <pre data-language="c">void* memcpy( void *dest, const void *src, size_t count );</pre> +</td> <td> <span class="t-mark-rev t-until-c99">(until C99)</span> </td> </tr> <tr class="t-dcl t-since-c99"> <td> <pre data-language="c">void* memcpy( void *restrict dest, const void *restrict src, size_t count );</pre> +</td> <td> <span class="t-mark-rev t-since-c99">(since C99)</span> </td> </tr> <tr class="t-dcl t-since-c11"> <td> <pre data-language="c">errno_t memcpy_s( void *restrict dest, rsize_t destsz, + const void *restrict src, rsize_t count );</pre> +</td> <td> (2) </td> <td> <span class="t-mark-rev t-since-c11">(since C11)</span> </td> </tr> </table> <div class="t-li1"> +<span class="t-li">1)</span> Copies <code>count</code> characters from the object pointed to by <code>src</code> to the object pointed to by <code>dest</code>. Both objects are interpreted as arrays of <code>unsigned char</code>.</div> <div class="t-li1"> + The behavior is undefined if access occurs beyond the end of the <code>dest</code> array. If the objects overlap<span class="t-rev-inl t-since-c99"><span> (which is a violation of the <a href="../../language/restrict" title="c/language/restrict"><code>restrict</code></a> contract)</span><span><span class="t-mark-rev t-since-c99">(since C99)</span></span></span>, the behavior is undefined. The behavior is undefined if either <code>dest</code> or <code>src</code> is an invalid or null pointer.</div> <div class="t-li1"> +<span class="t-li">2)</span> Same as <span class="t-v">(1)</span>, except that the following errors are detected at runtime and cause the entire destination range <code>[dest, dest+destsz)</code> to be zeroed out (if both <code>dest</code> and <code>destsz</code> are valid), as well as call the currently installed <a href="../../error/set_constraint_handler_s" title="c/error/set constraint handler s">constraint handler</a> function: <ul> +<li> <code>dest</code> or <code>src</code> is a null pointer </li> +<li> <code>destsz</code> or <code>count</code> is greater than <code>RSIZE_MAX</code> </li> +<li> <code>count</code> is greater than <code>destsz</code> (buffer overflow would occur) </li> +<li> the source and the destination objects overlap</li> +</ul> +</div> <div class="t-li1"> + The behavior is undefined if the size of the character array pointed to by <code>dest</code> < <code>count</code> <= <code>destsz</code>; in other words, an erroneous value of <code>destsz</code> does not expose the impending buffer overflow. As with all bounds-checked functions, <code>memcpy_s</code> only guaranteed to be available if <code>__STDC_LIB_EXT1__</code> is defined by the implementation and if the user defines <code>__STDC_WANT_LIB_EXT1__</code> to the integer constant <code>1</code> before including <a href="../byte" title="c/string/byte"><code><string.h></code></a>.</div> <h3 id="Parameters"> Parameters</h3> <table class="t-par-begin"> <tr class="t-par"> <td> dest </td> <td> - </td> <td> pointer to the object to copy to </td> +</tr> <tr class="t-par"> <td> destsz </td> <td> - </td> <td> max number of bytes to modify in the destination (typically the size of the destination object) </td> +</tr> <tr class="t-par"> <td> src </td> <td> - </td> <td> pointer to the object to copy from </td> +</tr> <tr class="t-par"> <td> count </td> <td> - </td> <td> number of bytes to copy </td> +</tr> +</table> <h3 id="Return_value"> Return value</h3> <div class="t-li1"> +<span class="t-li">1)</span> Returns a copy of <code>dest</code> +</div> <div class="t-li1"> +<span class="t-li">2)</span> Returns zero on success and non-zero value on error. Also on error, if <code>dest</code> is not a null pointer and <code>destsz</code> is valid, writes <code>destsz</code> zero bytes in to the destination array.</div> <h3 id="Notes"> Notes</h3> <p><code>memcpy</code> may be used to set the <a href="../../language/object#Effective_type" title="c/language/object">effective type</a> of an object obtained by an allocation function.</p> +<p><code>memcpy</code> is the fastest library routine for memory-to-memory copy. It is usually more efficient than <code><a href="strcpy" title="c/string/byte/strcpy">strcpy</a></code>, which must scan the data it copies or <code><a href="memmove" title="c/string/byte/memmove">memmove</a></code>, which must take precautions to handle overlapping inputs.</p> +<p>Several C compilers transform suitable memory-copying loops to <code>memcpy</code> calls.</p> +<p>Where <a href="../../language/object#Strict_aliasing" title="c/language/object">strict aliasing</a> prohibits examining the same memory as values of two different types, <code>memcpy</code> may be used to convert the values.</p> +<h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#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 usage + char 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 int + int *p = malloc(3*sizeof(int)); // allocated memory has no effective type + int arr[3] = {1,2,3}; + memcpy(p,arr,3*sizeof(int)); // allocated memory now has an effective type + + // reinterpreting data + double d = 0.1; +// int64_t n = *(int64_t*)(&d); // strict aliasing violation + int64_t n; + memcpy(&n, &d, sizeof d); // OK + printf("\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 destsz + printf("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 +}</pre></div> <p>Possible output:</p> +<div class="text source-text"><pre data-language="c">once +0x1.999999999999ap-4 is 3fb999999999999a as an int64_t +dst = "aaaaayxyxy", r = 0 +dst = "\0\0\0\0\0yxyxy", r = 22</pre></div> </div> <h3 id="References"> References</h3> <ul> +<li> C11 standard (ISO/IEC 9899:2011): </li> +<ul> +<li> 7.24.2.1 The memcpy function (p: 362) </li> +<li> K.3.7.1.1 The memcpy_s function (p: 614) </li> +</ul> +<li> C99 standard (ISO/IEC 9899:1999): </li> +<ul><li> 7.21.2.1 The memcpy function (p: 325) </li></ul> +<li> C89/C90 standard (ISO/IEC 9899:1990): </li> +<ul><li> 4.11.2.1 The memcpy function </li></ul> +</ul> <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td> <div><a href="memccpy" title="c/string/byte/memccpy"> <span class="t-lines"><span>memccpy</span></span></a></div> +<div><span class="t-lines"><span><span class="t-mark-rev t-since-c23">(C23)</span></span></span></div> </td> <td> copies one buffer to another, stopping after the specified delimiter <br> <span class="t-mark">(function)</span> </td> +</tr> <tr class="t-dsc"> <td> <div><a href="memmove" title="c/string/byte/memmove"> <span class="t-lines"><span>memmove</span><span>memmove_s</span></span></a></div> +<div><span class="t-lines"><span><span class="t-mark-rev t-since-c11">(C11)</span></span></span></div> </td> <td> moves one buffer to another <br> <span class="t-mark">(function)</span> </td> +</tr> <tr class="t-dsc"> <td> <div><a href="../wide/wmemcpy" title="c/string/wide/wmemcpy"> <span class="t-lines"><span>wmemcpy</span><span>wmemcpy_s</span></span></a></div> +<div><span class="t-lines"><span><span class="t-mark-rev t-since-c95">(C95)</span></span><span><span class="t-mark-rev t-since-c11">(C11)</span></span></span></div> </td> <td> copies a certain amount of wide characters between two non-overlapping arrays <br> <span class="t-mark">(function)</span> </td> +</tr> <tr class="t-dsc"> <td colspan="2"> <span><a href="https://en.cppreference.com/w/cpp/string/byte/memcpy" title="cpp/string/byte/memcpy">C++ documentation</a></span> for <code>memcpy</code> </td> +</tr> </table> <div class="_attribution"> + <p class="_attribution-p"> + © cppreference.com<br>Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.<br> + <a href="https://en.cppreference.com/w/c/string/byte/memcpy" class="_attribution-link">https://en.cppreference.com/w/c/string/byte/memcpy</a> + </p> +</div> |
