Difference between revisions of "cpp/string/byte/memset"
m (Undo previous revision: Not really sure) |
Andreas Krug (Talk | contribs) m ({{c}}, fmt) |
||
Line 1: | Line 1: | ||
− | {{cpp/title| memset}} | + | {{cpp/title|memset}} |
{{cpp/string/byte/navbar}} | {{cpp/string/byte/navbar}} | ||
− | {{ddcl | header=cstring | | + | {{ddcl|header=cstring| |
void* memset( void* dest, int ch, std::size_t count ); | void* memset( void* dest, int ch, std::size_t count ); | ||
}} | }} | ||
− | Copies the value {{c|static_cast<unsigned char>(ch)}} into each of the first {{ | + | Copies the value {{c|static_cast<unsigned char>(ch)}} into each of the first {{c|count}} characters of the object pointed to by {{c|dest}}. If the object is a [[cpp/language/object#Subobjects|potentially-overlapping subobject]] or is not {{named req|TriviallyCopyable}} (e.g., scalar, C-compatible struct, or an array of trivially copyable type), the behavior is undefined. If {{c|count}} is greater than the size of the object pointed to by {{c|dest}}, the behavior is undefined. |
===Parameters=== | ===Parameters=== | ||
{{par begin}} | {{par begin}} | ||
− | {{par | dest | pointer to the object to fill}} | + | {{par|dest|pointer to the object to fill}} |
− | {{par | ch | fill byte}} | + | {{par|ch|fill byte}} |
− | {{par | count | number of bytes to fill}} | + | {{par|count|number of bytes to fill}} |
{{par end}} | {{par end}} | ||
===Return value=== | ===Return value=== | ||
− | {{ | + | {{c|dest}} |
===Notes=== | ===Notes=== | ||
Line 24: | Line 24: | ||
===Example=== | ===Example=== | ||
{{example | {{example | ||
− | + | |code= | |
− | + | ||
#include <bitset> | #include <bitset> | ||
#include <climits> | #include <climits> | ||
Line 34: | Line 33: | ||
{ | { | ||
int a[4]; | int a[4]; | ||
− | using bits = std::bitset<sizeof(int)*CHAR_BIT>; | + | using bits = std::bitset<sizeof(int) * CHAR_BIT>; |
std::memset(a, 0b1111'0000'0011, sizeof a); | std::memset(a, 0b1111'0000'0011, sizeof a); | ||
− | for (int ai : a) std::cout << bits(ai) << '\n'; | + | for (int ai : a) |
+ | std::cout << bits(ai) << '\n'; | ||
} | } | ||
− | + | |output= | |
00000011000000110000001100000011 | 00000011000000110000001100000011 | ||
00000011000000110000001100000011 | 00000011000000110000001100000011 | ||
Line 47: | Line 47: | ||
===See also=== | ===See also=== | ||
{{dsc begin}} | {{dsc begin}} | ||
− | {{dsc inc | cpp/string/byte/dsc memcpy}} | + | {{dsc inc|cpp/string/byte/dsc memcpy}} |
− | {{dsc inc | cpp/string/byte/dsc memmove}} | + | {{dsc inc|cpp/string/byte/dsc memmove}} |
− | {{dsc inc | cpp/string/wide/dsc wmemset}} | + | {{dsc inc|cpp/string/wide/dsc wmemset}} |
− | {{dsc inc | cpp/algorithm/dsc fill}} | + | {{dsc inc|cpp/algorithm/dsc fill}} |
− | {{dsc inc | cpp/algorithm/dsc fill_n}} | + | {{dsc inc|cpp/algorithm/dsc fill_n}} |
− | {{dsc inc | cpp/types/dsc is_trivially_copyable}} | + | {{dsc inc|cpp/types/dsc is_trivially_copyable}} |
− | {{dsc see c | c/string/byte/memset}} | + | {{dsc see c|c/string/byte/memset}} |
{{dsc end}} | {{dsc end}} | ||
{{langlinks|de|es|fr|it|ja|pt|ru|zh}} | {{langlinks|de|es|fr|it|ja|pt|ru|zh}} |
Latest revision as of 09:31, 5 June 2023
Defined in header <cstring>
|
||
void* memset( void* dest, int ch, std::size_t count ); |
||
Copies the value static_cast<unsigned char>(ch) into each of the first count characters of the object pointed to by dest. If the object is a potentially-overlapping subobject or is not TriviallyCopyable (e.g., scalar, C-compatible struct, or an array of trivially copyable type), the behavior is undefined. If count is greater than the size of the object pointed to by dest, the behavior is undefined.
Contents |
[edit] Parameters
dest | - | pointer to the object to fill |
ch | - | fill byte |
count | - | number of bytes to fill |
[edit] Return value
dest
[edit] Notes
std::memset
may be optimized away (under the as-if rules) if the object modified by this function is not accessed again for the rest of its lifetime (e.g., gcc bug 8537). For that reason, this function cannot be used to scrub memory (e.g., to fill an array that stored a password with zeroes).
Solutions for that include std::fill with volatile pointers, (C23) memset_explicit(), (C11) memset_s, FreeBSD explicit_bzero or Microsoft SecureZeroMemory
.
[edit] Example
#include <bitset> #include <climits> #include <cstring> #include <iostream> int main() { int a[4]; using bits = std::bitset<sizeof(int) * CHAR_BIT>; std::memset(a, 0b1111'0000'0011, sizeof a); for (int ai : a) std::cout << bits(ai) << '\n'; }
Output:
00000011000000110000001100000011 00000011000000110000001100000011 00000011000000110000001100000011 00000011000000110000001100000011
[edit] See also
copies one buffer to another (function) | |
moves one buffer to another (function) | |
copies the given wide character to every position in a wide character array (function) | |
copy-assigns the given value to every element in a range (function template) | |
copy-assigns the given value to N elements in a range (function template) | |
(C++11) |
checks if a type is trivially copyable (class template) |
C documentation for memset
|