-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemset.html
More file actions
118 lines (98 loc) · 3.2 KB
/
memset.html
File metadata and controls
118 lines (98 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<!-- Mirrored from cppreference.com/stdstring/memset.html by HTTrack Website Copier/3.x [XR&CO'2004], Tue, 22 Jan 2008 06:23:56 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=iso-8859-1"><!-- /Added by HTTrack -->
<head>
<meta name="generator" content=
"HTML Tidy for Linux/x86 (vers 1 September 2005), see www.w3.org">
<title>memset</title>
<link href="../cppreference.css" rel="stylesheet" type="text/css">
<link href="../prettify.css" type="text/css" rel="stylesheet" />
<script type="text/javascript" src="../prettify.js"></script>
</head>
<body onload="prettyPrint()">
<table>
<tr>
<td>
<div class="body-content">
<div class="header-box">
<a href="../index-2.html">cppreference.com</a> > <a href=
"index.html">Standard C String and Character</a> > <a href=
"memset.html">memset</a>
</div>
<div class="name-format">
memset
</div>
<div class="syntax-name-format">
Syntax:
</div>
<pre class="syntax-box">
#include <cstring>
void* memset( void* buffer, int ch, size_t count );
</pre>
<p>The function memset() copies <em>ch</em> into the first
<em>count</em> characters of <em>buffer</em>, and returns
<em>buffer</em>. memset() is useful for intializing a section of
memory to some value. For example, this command:</p>
<pre class="prettyprint">
const int ARRAY_LENGTH;
char the_array[ARRAY_LENGTH];
...
// zero out the contents of the_array
memset( the_array, '\0', ARRAY_LENGTH );
</pre>
<p>...is a very efficient way to set all values of the_array to
zero.</p>
<p>The table below compares two different methods for initializing
an array of characters: a for-loop versus memset(). As the size of the
data being initialized increases, memset() clearly gets the job done
much more quickly:</p>
<table class="misc-table">
<tr class="misc-table-tr-1">
<th>Input size</th>
<th>Initialized with a for-loop</th>
<th>Initialized with memset()</th>
</tr>
<tr class="misc-table-tr-2">
<td>1000</td>
<td>0.016</td>
<td>0.017</td>
</tr>
<tr class="misc-table-tr-1">
<td>10000</td>
<td>0.055</td>
<td>0.013</td>
</tr>
<tr class="misc-table-tr-2">
<td>100000</td>
<td>0.443</td>
<td>0.029</td>
</tr>
<tr class="misc-table-tr-1">
<td>1000000</td>
<td>4.337</td>
<td>0.291</td>
</tr>
</table>
<div class="related-name-format">
Related topics:
</div>
<div class="related-content">
<a href="memcmp.html">memcmp</a><br>
<a href="memcpy.html">memcpy</a><br>
<a href="memmove.html">memmove</a>
</div>
</div>
</td>
<script src="../../www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-2828341-1";
urchinTracker();
</script>
</tr>
</table>
</body>
<!-- Mirrored from cppreference.com/stdstring/memset.html by HTTrack Website Copier/3.x [XR&CO'2004], Tue, 22 Jan 2008 06:23:56 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=iso-8859-1"><!-- /Added by HTTrack -->
</html>