diff options
| author | Craig Jennings <c@cjennings.net> | 2024-04-07 13:41:34 -0500 |
|---|---|---|
| committer | Craig Jennings <c@cjennings.net> | 2024-04-07 13:41:34 -0500 |
| commit | 754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 (patch) | |
| tree | f1190704f78f04a2b0b4c977d20fe96a828377f1 /devdocs/c/string%2Fbyte%2Fmemmove.html | |
new repository
Diffstat (limited to 'devdocs/c/string%2Fbyte%2Fmemmove.html')
| -rw-r--r-- | devdocs/c/string%2Fbyte%2Fmemmove.html | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/devdocs/c/string%2Fbyte%2Fmemmove.html b/devdocs/c/string%2Fbyte%2Fmemmove.html new file mode 100644 index 00000000..2fbb10a9 --- /dev/null +++ b/devdocs/c/string%2Fbyte%2Fmemmove.html @@ -0,0 +1,93 @@ + <h1 id="firstHeading" class="firstHeading">memmove, memmove_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"> <td> <pre data-language="c">void* memmove( void* dest, const void* src, size_t count );</pre> +</td> <td> (1) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl t-since-c11"> <td> <pre data-language="c">errno_t memmove_s(void *dest, rsize_t destsz, const void *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>. 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 to <code>dest</code>.</div> <div class="t-li1"> + The behavior is undefined if access occurs beyond the end of the dest array. 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 when detecting the following errors at runtime, it zeroes out the entire destination range <code>[dest, dest+destsz)</code> (if both <code>dest</code> and <code>destsz</code> are valid) and calls 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> +</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>memmove_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>memmove</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>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 efficient <code><a href="memcpy" title="c/string/byte/memcpy">memcpy</a></code> when there is no overlap at all.</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>memmove</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) +{ + 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 int + int *p = malloc(3*sizeof(int)); // allocated memory has no effective type + int arr[3] = {1,2,3}; + memmove(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; + memmove(&n, &d, sizeof d); // OK + printf("%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 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">1234567890 +1234456890 +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> C17 standard (ISO/IEC 9899:2018): </li> +<ul> +<li> 7.24.2.2 The memmove function (p: 264) </li> +<li> K.3.7.1.2 The memmove_s function (p: 446) </li> +</ul> +<li> C11 standard (ISO/IEC 9899:2011): </li> +<ul> +<li> 7.24.2.2 The memmove function (p: 363) </li> +<li> K.3.7.1.2 The memmove_s function (p: 615) </li> +</ul> +<li> C99 standard (ISO/IEC 9899:1999): </li> +<ul><li> 7.21.2.2 The memmove function (p: 326) </li></ul> +<li> C89/C90 standard (ISO/IEC 9899:1990): </li> +<ul><li> 4.11.2.2 The memmove function </li></ul> +</ul> <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td> <div><a href="memcpy" title="c/string/byte/memcpy"> <span class="t-lines"><span>memcpy</span><span>memcpy_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> copies one buffer to another <br> <span class="t-mark">(function)</span> </td> +</tr> <tr class="t-dsc"> <td> <div><a href="../wide/wmemmove" title="c/string/wide/wmemmove"> <span class="t-lines"><span>wmemmove</span><span>wmemmove_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, possibly 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/memmove" title="cpp/string/byte/memmove">C++ documentation</a></span> for <code>memmove</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/memmove" class="_attribution-link">https://en.cppreference.com/w/c/string/byte/memmove</a> + </p> +</div> |
