diff options
Diffstat (limited to 'devdocs/c/string%2Fbyte%2Fmemset.html')
| -rw-r--r-- | devdocs/c/string%2Fbyte%2Fmemset.html | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/devdocs/c/string%2Fbyte%2Fmemset.html b/devdocs/c/string%2Fbyte%2Fmemset.html new file mode 100644 index 00000000..cf822c20 --- /dev/null +++ b/devdocs/c/string%2Fbyte%2Fmemset.html @@ -0,0 +1,73 @@ + <h1 id="firstHeading" class="firstHeading">memset, memset_explicit, memset_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 *memset( void *dest, int ch, size_t count );</pre> +</td> <td> (1) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl t-since-c23"> <td> <pre data-language="c">void *memset_explicit( void *dest, int ch, size_t count );</pre> +</td> <td> (2) </td> <td> <span class="t-mark-rev t-since-c23">(since C23)</span> </td> </tr> <tr class="t-dcl t-since-c11"> <td> <pre data-language="c">errno_t memset_s( void *dest, rsize_t destsz, int ch, rsize_t count );</pre> +</td> <td> (3) </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 the value <code>(unsigned char)ch</code> into each of the first <code>count</code> characters of the object pointed to by <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 <code>dest</code> is a null pointer.</div> <div class="t-li1"> +<span class="t-li">2)</span> Same as <span class="t-v">(1)</span>, except that is safe for sensitive information.</div> <div class="t-li1"> +<span class="t-li">3)</span> Same as <span class="t-v">(1)</span>, except that the following errors are detected at runtime and call the currently installed <a href="../../error/set_constraint_handler_s" title="c/error/set constraint handler s">constraint handler</a> function after storing <code>ch</code> in every location of the destination range <code>[dest, dest+destsz)</code> if <code>dest</code> and <code>destsz</code> are themselves valid: <ul> +<li> <code>dest</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>memset_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 fill </td> +</tr> <tr class="t-par"> <td> ch </td> <td> - </td> <td> fill byte </td> +</tr> <tr class="t-par"> <td> count </td> <td> - </td> <td> number of bytes to fill </td> +</tr> <tr class="t-par"> <td> destsz </td> <td> - </td> <td> size of the destination array </td> +</tr> +</table> <h3 id="Return_value"> Return value</h3> <div class="t-li1"> +<span class="t-li">1,2)</span> A copy of <code>dest</code> +</div> <div class="t-li1"> +<span class="t-li">3)</span> zero on success, non-zero on error. Also on error, if <code>dest</code> is not a null pointer and <code>destsz</code> is valid, writes <code>destsz</code> fill bytes <code>ch</code> to the destination array.</div> <h3 id="Notes"> Notes</h3> <p><code>memset</code> may be optimized away (under the <a href="../../language/as_if" title="c/language/as if">as-if</a> rules) if the object modified by this function is not accessed again for the rest of its lifetime (e.g., <a rel="nofollow" class="external text" href="https://gcc.gnu.org/bugzilla/show_bug.cgi?id=8537">gcc bug 8537</a>). For that reason, this function cannot be used to scrub memory (e.g., to fill an array that stored a password with zeroes).</p> +<p>This optimization is prohibited for <code>memset_explicit</code> and <code>memset_s</code>: they are guaranteed to perform the memory write.</p> +<p>Third-party solutions for that include FreeBSD <a rel="nofollow" class="external text" href="https://www.freebsd.org/cgi/man.cgi?query=explicit_bzero"><code>explicit_bzero</code></a> or Microsoft <a rel="nofollow" class="external text" href="https://msdn.microsoft.com/en-us/library/windows/desktop/aa366877.aspx"><code>SecureZeroMemory</code></a>.</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 <string.h> +#include <stdlib.h> + +int main(void) +{ + char str[] = "ghghghghghghghghghghgh"; + puts(str); + memset(str,'a',5); + puts(str); + +#ifdef __STDC_LIB_EXT1__ + set_constraint_handler_s(ignore_handler_s); + int r = memset_s(str, sizeof str, 'b', 5); + printf("str = \"%s\", r = %d\n", str, r); + r = memset_s(str, 5, 'c', 10); // count is greater than destsz + printf("str = \"%s\", r = %d\n", str, r); +#endif +}</pre></div> <p>Possible output:</p> +<div class="text source-text"><pre data-language="c">ghghghghghghghghghghgh +aaaaahghghghghghghghgh +str = "bbbbbhghghghghghghghgh", r = 0 +str = "ccccchghghghghghghghgh", r = 22</pre></div> </div> <h3 id="References"> References</h3> <ul> +<li> C17 standard (ISO/IEC 9899:2018): </li> +<ul> +<li> 7.24.6.1 The memset function (p: 270) </li> +<li> K.3.7.4.1 The memset_s function (p: 451) </li> +</ul> +<li> C11 standard (ISO/IEC 9899:2011): </li> +<ul> +<li> 7.24.6.1 The memset function (p: 371) </li> +<li> K.3.7.4.1 The memset_s function (p: 621-622) </li> +</ul> +<li> C99 standard (ISO/IEC 9899:1999): </li> +<ul><li> 7.21.6.1 The memset function (p: 333) </li></ul> +<li> C89/C90 standard (ISO/IEC 9899:1990): </li> +<ul><li> 4.11.6.1 The memset 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/wmemset" title="c/string/wide/wmemset"> <span class="t-lines"><span>wmemset</span></span></a></div> +<div><span class="t-lines"><span><span class="t-mark-rev t-since-c95">(C95)</span></span></span></div> </td> <td> copies the given wide character to every position in a wide character array <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/memset" title="cpp/string/byte/memset">C++ documentation</a></span> for <code>memset</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/memset" class="_attribution-link">https://en.cppreference.com/w/c/string/byte/memset</a> + </p> +</div> |
