Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/string/byte/memset"

From cppreference.com
< cpp‎ | string‎ | byte
(note about scrubbing from C)
(See also: std::fill)
Line 41: Line 41:
 
{{dsc inc | cpp/string/byte/dsc memcpy}}
 
{{dsc inc | cpp/string/byte/dsc memcpy}}
 
{{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_n}}
 
{{dsc inc | cpp/algorithm/dsc fill_n}}
 
{{dsc inc | cpp/types/dsc is_trivially_copyable}}
 
{{dsc inc | cpp/types/dsc is_trivially_copyable}}

Revision as of 08:04, 17 March 2015

Defined in header <cstring>
void* memset( void* dest, int ch, std::size_t count );

Converts the value ch to unsigned char and copies it into each of the first count characters of the object pointed to by dest. If the object is not trivially-copyable (e.g., scalar, array, or a C-compatible struct), the behavior is undefined. If count is greater than the size of the object pointed to by dest, the behavior is undefined.

Contents

Parameters

dest - pointer to the object to fill
ch - fill byte
count - number of bytes to fill

Return value

dest

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. For that reason, this function cannot be used to scrub memory (e.g. to fill an array that stored a password with zeroes).

Example

#include <iostream>
#include <cstring>
 
int main()
{
    int a[20];
    std::memset(a, 0, sizeof a);
    for (int ai : a) std::cout << ai;
}

Output:

00000000000000000000

See also

copies one buffer to another
(function) [edit]
copies the given wide character to every position in a wide character array
(function) [edit]
copy-assigns the given value to every element in a range
(function template) [edit]
copy-assigns the given value to N elements in a range
(function template) [edit]
checks if a type is trivially copyable
(class template) [edit]
C documentation for memset