std::memset - cppreference.com

##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

# std::memset

From cppreference.com
< cpp‎ | string‎ | byte

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.

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

### [edit] Example

Run this code

    #include <iostream>
    #include <cstring>

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

Output:

    00000000000000000000

### [edit] See also

memcpy | copies one buffer to another (function) [edit]
wmemset | copies the given wide character to every position in a wide character array (function) [edit]
fill | assigns a range of elements a certain value (function template) [edit]
fill_n | assigns a value to a number of elements (function template) [edit]
is_trivially_copyable (C++11) | checks if a type is trivially copyable (class template) [edit]
C documentation for memset

Retrieved from "http://en.cppreference.com/mwiki/index.php?title=cpp/string/byte/memset&oldid=77177"