• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<html>
2<head>
3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4<title>buffer</title>
5<link rel="stylesheet" href="../../../../doc/src/boostbook.css" type="text/css">
6<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
7<link rel="home" href="../../boost_asio.html" title="Boost.Asio">
8<link rel="up" href="../reference.html" title="Reference">
9<link rel="prev" href="bind_executor/overload2.html" title="bind_executor (2 of 2 overloads)">
10<link rel="next" href="buffer/overload1.html" title="buffer (1 of 32 overloads)">
11</head>
12<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
13<table cellpadding="2" width="100%"><tr>
14<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../boost.png"></td>
15<td align="center"><a href="../../../../index.html">Home</a></td>
16<td align="center"><a href="../../../../libs/libraries.htm">Libraries</a></td>
17<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
18<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
19<td align="center"><a href="../../../../more/index.htm">More</a></td>
20</tr></table>
21<hr>
22<div class="spirit-nav">
23<a accesskey="p" href="bind_executor/overload2.html"><img src="../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../reference.html"><img src="../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../boost_asio.html"><img src="../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="buffer/overload1.html"><img src="../../../../doc/src/images/next.png" alt="Next"></a>
24</div>
25<div class="section">
26<div class="titlepage"><div><div><h3 class="title">
27<a name="boost_asio.reference.buffer"></a><a class="link" href="buffer.html" title="buffer">buffer</a>
28</h3></div></div></div>
29<p>
30        <a class="indexterm" name="boost_asio.indexterm.buffer"></a>
31The <code class="computeroutput">boost::asio::buffer</code> function
32        is used to create a buffer object to represent raw memory, an array of POD
33        elements, a vector of POD elements, or a std::string.
34      </p>
35<p>
36        Create a new modifiable buffer from an existing buffer.
37      </p>
38<pre class="programlisting">mutable_buffer <a class="link" href="buffer/overload1.html" title="buffer (1 of 32 overloads)">buffer</a>(
39    const mutable_buffer &amp; b);
40  <span class="emphasis"><em>» <a class="link" href="buffer/overload1.html" title="buffer (1 of 32 overloads)">more...</a></em></span>
41
42mutable_buffer <a class="link" href="buffer/overload2.html" title="buffer (2 of 32 overloads)">buffer</a>(
43    const mutable_buffer &amp; b,
44    std::size_t max_size_in_bytes);
45  <span class="emphasis"><em>» <a class="link" href="buffer/overload2.html" title="buffer (2 of 32 overloads)">more...</a></em></span>
46</pre>
47<p>
48        Create a new non-modifiable buffer from an existing buffer.
49      </p>
50<pre class="programlisting">const_buffer <a class="link" href="buffer/overload3.html" title="buffer (3 of 32 overloads)">buffer</a>(
51    const const_buffer &amp; b);
52  <span class="emphasis"><em>» <a class="link" href="buffer/overload3.html" title="buffer (3 of 32 overloads)">more...</a></em></span>
53
54const_buffer <a class="link" href="buffer/overload4.html" title="buffer (4 of 32 overloads)">buffer</a>(
55    const const_buffer &amp; b,
56    std::size_t max_size_in_bytes);
57  <span class="emphasis"><em>» <a class="link" href="buffer/overload4.html" title="buffer (4 of 32 overloads)">more...</a></em></span>
58</pre>
59<p>
60        Create a new modifiable buffer that represents the given memory range.
61      </p>
62<pre class="programlisting">mutable_buffer <a class="link" href="buffer/overload5.html" title="buffer (5 of 32 overloads)">buffer</a>(
63    void * data,
64    std::size_t size_in_bytes);
65  <span class="emphasis"><em>» <a class="link" href="buffer/overload5.html" title="buffer (5 of 32 overloads)">more...</a></em></span>
66</pre>
67<p>
68        Create a new non-modifiable buffer that represents the given memory range.
69      </p>
70<pre class="programlisting">const_buffer <a class="link" href="buffer/overload6.html" title="buffer (6 of 32 overloads)">buffer</a>(
71    const void * data,
72    std::size_t size_in_bytes);
73  <span class="emphasis"><em>» <a class="link" href="buffer/overload6.html" title="buffer (6 of 32 overloads)">more...</a></em></span>
74</pre>
75<p>
76        Create a new modifiable buffer that represents the given POD array.
77      </p>
78<pre class="programlisting">template&lt;
79    typename PodType,
80    std::size_t N&gt;
81mutable_buffer <a class="link" href="buffer/overload7.html" title="buffer (7 of 32 overloads)">buffer</a>(
82    PodType (&amp;data)[N]);
83  <span class="emphasis"><em>» <a class="link" href="buffer/overload7.html" title="buffer (7 of 32 overloads)">more...</a></em></span>
84
85template&lt;
86    typename PodType,
87    std::size_t N&gt;
88mutable_buffer <a class="link" href="buffer/overload8.html" title="buffer (8 of 32 overloads)">buffer</a>(
89    PodType (&amp;data)[N],
90    std::size_t max_size_in_bytes);
91  <span class="emphasis"><em>» <a class="link" href="buffer/overload8.html" title="buffer (8 of 32 overloads)">more...</a></em></span>
92</pre>
93<p>
94        Create a new non-modifiable buffer that represents the given POD array.
95      </p>
96<pre class="programlisting">template&lt;
97    typename PodType,
98    std::size_t N&gt;
99const_buffer <a class="link" href="buffer/overload9.html" title="buffer (9 of 32 overloads)">buffer</a>(
100    const PodType (&amp;data)[N]);
101  <span class="emphasis"><em>» <a class="link" href="buffer/overload9.html" title="buffer (9 of 32 overloads)">more...</a></em></span>
102
103template&lt;
104    typename PodType,
105    std::size_t N&gt;
106const_buffer <a class="link" href="buffer/overload10.html" title="buffer (10 of 32 overloads)">buffer</a>(
107    const PodType (&amp;data)[N],
108    std::size_t max_size_in_bytes);
109  <span class="emphasis"><em>» <a class="link" href="buffer/overload10.html" title="buffer (10 of 32 overloads)">more...</a></em></span>
110</pre>
111<p>
112        Create a new modifiable buffer that represents the given POD array.
113      </p>
114<pre class="programlisting">template&lt;
115    typename PodType,
116    std::size_t N&gt;
117mutable_buffer <a class="link" href="buffer/overload11.html" title="buffer (11 of 32 overloads)">buffer</a>(
118    boost::array&lt; PodType, N &gt; &amp; data);
119  <span class="emphasis"><em>» <a class="link" href="buffer/overload11.html" title="buffer (11 of 32 overloads)">more...</a></em></span>
120
121template&lt;
122    typename PodType,
123    std::size_t N&gt;
124mutable_buffer <a class="link" href="buffer/overload12.html" title="buffer (12 of 32 overloads)">buffer</a>(
125    boost::array&lt; PodType, N &gt; &amp; data,
126    std::size_t max_size_in_bytes);
127  <span class="emphasis"><em>» <a class="link" href="buffer/overload12.html" title="buffer (12 of 32 overloads)">more...</a></em></span>
128</pre>
129<p>
130        Create a new non-modifiable buffer that represents the given POD array.
131      </p>
132<pre class="programlisting">template&lt;
133    typename PodType,
134    std::size_t N&gt;
135const_buffer <a class="link" href="buffer/overload13.html" title="buffer (13 of 32 overloads)">buffer</a>(
136    boost::array&lt; const PodType, N &gt; &amp; data);
137  <span class="emphasis"><em>» <a class="link" href="buffer/overload13.html" title="buffer (13 of 32 overloads)">more...</a></em></span>
138
139template&lt;
140    typename PodType,
141    std::size_t N&gt;
142const_buffer <a class="link" href="buffer/overload14.html" title="buffer (14 of 32 overloads)">buffer</a>(
143    boost::array&lt; const PodType, N &gt; &amp; data,
144    std::size_t max_size_in_bytes);
145  <span class="emphasis"><em>» <a class="link" href="buffer/overload14.html" title="buffer (14 of 32 overloads)">more...</a></em></span>
146
147template&lt;
148    typename PodType,
149    std::size_t N&gt;
150const_buffer <a class="link" href="buffer/overload15.html" title="buffer (15 of 32 overloads)">buffer</a>(
151    const boost::array&lt; PodType, N &gt; &amp; data);
152  <span class="emphasis"><em>» <a class="link" href="buffer/overload15.html" title="buffer (15 of 32 overloads)">more...</a></em></span>
153
154template&lt;
155    typename PodType,
156    std::size_t N&gt;
157const_buffer <a class="link" href="buffer/overload16.html" title="buffer (16 of 32 overloads)">buffer</a>(
158    const boost::array&lt; PodType, N &gt; &amp; data,
159    std::size_t max_size_in_bytes);
160  <span class="emphasis"><em>» <a class="link" href="buffer/overload16.html" title="buffer (16 of 32 overloads)">more...</a></em></span>
161</pre>
162<p>
163        Create a new modifiable buffer that represents the given POD array.
164      </p>
165<pre class="programlisting">template&lt;
166    typename PodType,
167    std::size_t N&gt;
168mutable_buffer <a class="link" href="buffer/overload17.html" title="buffer (17 of 32 overloads)">buffer</a>(
169    std::array&lt; PodType, N &gt; &amp; data);
170  <span class="emphasis"><em>» <a class="link" href="buffer/overload17.html" title="buffer (17 of 32 overloads)">more...</a></em></span>
171
172template&lt;
173    typename PodType,
174    std::size_t N&gt;
175mutable_buffer <a class="link" href="buffer/overload18.html" title="buffer (18 of 32 overloads)">buffer</a>(
176    std::array&lt; PodType, N &gt; &amp; data,
177    std::size_t max_size_in_bytes);
178  <span class="emphasis"><em>» <a class="link" href="buffer/overload18.html" title="buffer (18 of 32 overloads)">more...</a></em></span>
179</pre>
180<p>
181        Create a new non-modifiable buffer that represents the given POD array.
182      </p>
183<pre class="programlisting">template&lt;
184    typename PodType,
185    std::size_t N&gt;
186const_buffer <a class="link" href="buffer/overload19.html" title="buffer (19 of 32 overloads)">buffer</a>(
187    std::array&lt; const PodType, N &gt; &amp; data);
188  <span class="emphasis"><em>» <a class="link" href="buffer/overload19.html" title="buffer (19 of 32 overloads)">more...</a></em></span>
189
190template&lt;
191    typename PodType,
192    std::size_t N&gt;
193const_buffer <a class="link" href="buffer/overload20.html" title="buffer (20 of 32 overloads)">buffer</a>(
194    std::array&lt; const PodType, N &gt; &amp; data,
195    std::size_t max_size_in_bytes);
196  <span class="emphasis"><em>» <a class="link" href="buffer/overload20.html" title="buffer (20 of 32 overloads)">more...</a></em></span>
197
198template&lt;
199    typename PodType,
200    std::size_t N&gt;
201const_buffer <a class="link" href="buffer/overload21.html" title="buffer (21 of 32 overloads)">buffer</a>(
202    const std::array&lt; PodType, N &gt; &amp; data);
203  <span class="emphasis"><em>» <a class="link" href="buffer/overload21.html" title="buffer (21 of 32 overloads)">more...</a></em></span>
204
205template&lt;
206    typename PodType,
207    std::size_t N&gt;
208const_buffer <a class="link" href="buffer/overload22.html" title="buffer (22 of 32 overloads)">buffer</a>(
209    const std::array&lt; PodType, N &gt; &amp; data,
210    std::size_t max_size_in_bytes);
211  <span class="emphasis"><em>» <a class="link" href="buffer/overload22.html" title="buffer (22 of 32 overloads)">more...</a></em></span>
212</pre>
213<p>
214        Create a new modifiable buffer that represents the given POD vector.
215      </p>
216<pre class="programlisting">template&lt;
217    typename PodType,
218    typename Allocator&gt;
219mutable_buffer <a class="link" href="buffer/overload23.html" title="buffer (23 of 32 overloads)">buffer</a>(
220    std::vector&lt; PodType, Allocator &gt; &amp; data);
221  <span class="emphasis"><em>» <a class="link" href="buffer/overload23.html" title="buffer (23 of 32 overloads)">more...</a></em></span>
222
223template&lt;
224    typename PodType,
225    typename Allocator&gt;
226mutable_buffer <a class="link" href="buffer/overload24.html" title="buffer (24 of 32 overloads)">buffer</a>(
227    std::vector&lt; PodType, Allocator &gt; &amp; data,
228    std::size_t max_size_in_bytes);
229  <span class="emphasis"><em>» <a class="link" href="buffer/overload24.html" title="buffer (24 of 32 overloads)">more...</a></em></span>
230</pre>
231<p>
232        Create a new non-modifiable buffer that represents the given POD vector.
233      </p>
234<pre class="programlisting">template&lt;
235    typename PodType,
236    typename Allocator&gt;
237const_buffer <a class="link" href="buffer/overload25.html" title="buffer (25 of 32 overloads)">buffer</a>(
238    const std::vector&lt; PodType, Allocator &gt; &amp; data);
239  <span class="emphasis"><em>» <a class="link" href="buffer/overload25.html" title="buffer (25 of 32 overloads)">more...</a></em></span>
240
241template&lt;
242    typename PodType,
243    typename Allocator&gt;
244const_buffer <a class="link" href="buffer/overload26.html" title="buffer (26 of 32 overloads)">buffer</a>(
245    const std::vector&lt; PodType, Allocator &gt; &amp; data,
246    std::size_t max_size_in_bytes);
247  <span class="emphasis"><em>» <a class="link" href="buffer/overload26.html" title="buffer (26 of 32 overloads)">more...</a></em></span>
248</pre>
249<p>
250        Create a new modifiable buffer that represents the given string.
251      </p>
252<pre class="programlisting">template&lt;
253    typename Elem,
254    typename Traits,
255    typename Allocator&gt;
256mutable_buffer <a class="link" href="buffer/overload27.html" title="buffer (27 of 32 overloads)">buffer</a>(
257    std::basic_string&lt; Elem, Traits, Allocator &gt; &amp; data);
258  <span class="emphasis"><em>» <a class="link" href="buffer/overload27.html" title="buffer (27 of 32 overloads)">more...</a></em></span>
259
260template&lt;
261    typename Elem,
262    typename Traits,
263    typename Allocator&gt;
264mutable_buffer <a class="link" href="buffer/overload28.html" title="buffer (28 of 32 overloads)">buffer</a>(
265    std::basic_string&lt; Elem, Traits, Allocator &gt; &amp; data,
266    std::size_t max_size_in_bytes);
267  <span class="emphasis"><em>» <a class="link" href="buffer/overload28.html" title="buffer (28 of 32 overloads)">more...</a></em></span>
268</pre>
269<p>
270        Create a new non-modifiable buffer that represents the given string.
271      </p>
272<pre class="programlisting">template&lt;
273    typename Elem,
274    typename Traits,
275    typename Allocator&gt;
276const_buffer <a class="link" href="buffer/overload29.html" title="buffer (29 of 32 overloads)">buffer</a>(
277    const std::basic_string&lt; Elem, Traits, Allocator &gt; &amp; data);
278  <span class="emphasis"><em>» <a class="link" href="buffer/overload29.html" title="buffer (29 of 32 overloads)">more...</a></em></span>
279
280template&lt;
281    typename Elem,
282    typename Traits,
283    typename Allocator&gt;
284const_buffer <a class="link" href="buffer/overload30.html" title="buffer (30 of 32 overloads)">buffer</a>(
285    const std::basic_string&lt; Elem, Traits, Allocator &gt; &amp; data,
286    std::size_t max_size_in_bytes);
287  <span class="emphasis"><em>» <a class="link" href="buffer/overload30.html" title="buffer (30 of 32 overloads)">more...</a></em></span>
288</pre>
289<p>
290        Create a new modifiable buffer that represents the given string_view.
291      </p>
292<pre class="programlisting">template&lt;
293    typename Elem,
294    typename Traits&gt;
295const_buffer <a class="link" href="buffer/overload31.html" title="buffer (31 of 32 overloads)">buffer</a>(
296    basic_string_view&lt; Elem, Traits &gt; data);
297  <span class="emphasis"><em>» <a class="link" href="buffer/overload31.html" title="buffer (31 of 32 overloads)">more...</a></em></span>
298</pre>
299<p>
300        Create a new non-modifiable buffer that represents the given string.
301      </p>
302<pre class="programlisting">template&lt;
303    typename Elem,
304    typename Traits&gt;
305const_buffer <a class="link" href="buffer/overload32.html" title="buffer (32 of 32 overloads)">buffer</a>(
306    basic_string_view&lt; Elem, Traits &gt; data,
307    std::size_t max_size_in_bytes);
308  <span class="emphasis"><em>» <a class="link" href="buffer/overload32.html" title="buffer (32 of 32 overloads)">more...</a></em></span>
309</pre>
310<p>
311        A buffer object represents a contiguous region of memory as a 2-tuple consisting
312        of a pointer and size in bytes. A tuple of the form <code class="computeroutput">{void*, size_t}</code>
313        specifies a mutable (modifiable) region of memory. Similarly, a tuple of
314        the form <code class="computeroutput">{const void*, size_t}</code> specifies a const (non-modifiable)
315        region of memory. These two forms correspond to the classes <a class="link" href="mutable_buffer.html" title="mutable_buffer"><code class="computeroutput">mutable_buffer</code></a>
316        and <a class="link" href="const_buffer.html" title="const_buffer"><code class="computeroutput">const_buffer</code></a>,
317        respectively. To mirror C++'s conversion rules, a <a class="link" href="mutable_buffer.html" title="mutable_buffer"><code class="computeroutput">mutable_buffer</code></a>
318        is implicitly convertible to a <a class="link" href="const_buffer.html" title="const_buffer"><code class="computeroutput">const_buffer</code></a>,
319        and the opposite conversion is not permitted.
320      </p>
321<p>
322        The simplest use case involves reading or writing a single buffer of a specified
323        size:
324      </p>
325<pre class="programlisting">sock.send(boost::asio::buffer(data, size));
326</pre>
327<p>
328        In the above example, the return value of <code class="computeroutput">boost::asio::buffer</code>
329        meets the requirements of the ConstBufferSequence concept so that it may
330        be directly passed to the socket's write function. A buffer created for modifiable
331        memory also meets the requirements of the MutableBufferSequence concept.
332      </p>
333<p>
334        An individual buffer may be created from a builtin array, std::vector, std::array
335        or boost::array of POD elements. This helps prevent buffer overruns by automatically
336        determining the size of the buffer:
337      </p>
338<pre class="programlisting">char d1[128];
339size_t bytes_transferred = sock.receive(boost::asio::buffer(d1));
340
341std::vector&lt;char&gt; d2(128);
342bytes_transferred = sock.receive(boost::asio::buffer(d2));
343
344std::array&lt;char, 128&gt; d3;
345bytes_transferred = sock.receive(boost::asio::buffer(d3));
346
347boost::array&lt;char, 128&gt; d4;
348bytes_transferred = sock.receive(boost::asio::buffer(d4));
349</pre>
350<p>
351        In all three cases above, the buffers created are exactly 128 bytes long.
352        Note that a vector is <span class="emphasis"><em>never</em></span> automatically resized when
353        creating or using a buffer. The buffer size is determined using the vector's
354        <code class="computeroutput">size()</code> member function, and not its capacity.
355      </p>
356<h5>
357<a name="boost_asio.reference.buffer.h0"></a>
358        <span class="phrase"><a name="boost_asio.reference.buffer.accessing_buffer_contents"></a></span><a class="link" href="buffer.html#boost_asio.reference.buffer.accessing_buffer_contents">Accessing
359        Buffer Contents</a>
360      </h5>
361<p>
362        The contents of a buffer may be accessed using the <code class="computeroutput">data()</code> and
363        <code class="computeroutput">size()</code> member functions:
364      </p>
365<pre class="programlisting">boost::asio::mutable_buffer b1 = ...;
366std::size_t s1 = b1.size();
367unsigned char* p1 = static_cast&lt;unsigned char*&gt;(b1.data());
368
369boost::asio::const_buffer b2 = ...;
370std::size_t s2 = b2.size();
371const void* p2 = b2.data();
372</pre>
373<p>
374        The <code class="computeroutput">data()</code> member function permits violations of type safety,
375        so uses of it in application code should be carefully considered.
376      </p>
377<p>
378        For convenience, a <a class="link" href="buffer_size.html" title="buffer_size"><code class="computeroutput">buffer_size</code></a>
379        function is provided that works with both buffers and buffer sequences (that
380        is, types meeting the ConstBufferSequence or MutableBufferSequence type requirements).
381        In this case, the function returns the total size of all buffers in the sequence.
382      </p>
383<h5>
384<a name="boost_asio.reference.buffer.h1"></a>
385        <span class="phrase"><a name="boost_asio.reference.buffer.buffer_copying"></a></span><a class="link" href="buffer.html#boost_asio.reference.buffer.buffer_copying">Buffer
386        Copying</a>
387      </h5>
388<p>
389        The <a class="link" href="buffer_copy.html" title="buffer_copy"><code class="computeroutput">buffer_copy</code></a>
390        function may be used to copy raw bytes between individual buffers and buffer
391        sequences.
392      </p>
393<p>
394        In particular, when used with the <a class="link" href="buffer_size.html" title="buffer_size"><code class="computeroutput">buffer_size</code></a>
395        function, the <a class="link" href="buffer_copy.html" title="buffer_copy"><code class="computeroutput">buffer_copy</code></a>
396        function can be used to linearise a sequence of buffers. For example:
397      </p>
398<pre class="programlisting">vector&lt;const_buffer&gt; buffers = ...;
399
400vector&lt;unsigned char&gt; data(boost::asio::buffer_size(buffers));
401boost::asio::buffer_copy(boost::asio::buffer(data), buffers);
402</pre>
403<p>
404        Note that <a class="link" href="buffer_copy.html" title="buffer_copy"><code class="computeroutput">buffer_copy</code></a>
405        is implemented in terms of <code class="computeroutput">memcpy</code>, and consequently it cannot
406        be used to copy between overlapping memory regions.
407      </p>
408<h5>
409<a name="boost_asio.reference.buffer.h2"></a>
410        <span class="phrase"><a name="boost_asio.reference.buffer.buffer_invalidation"></a></span><a class="link" href="buffer.html#boost_asio.reference.buffer.buffer_invalidation">Buffer
411        Invalidation</a>
412      </h5>
413<p>
414        A buffer object does not have any ownership of the memory it refers to. It
415        is the responsibility of the application to ensure the memory region remains
416        valid until it is no longer required for an I/O operation. When the memory
417        is no longer available, the buffer is said to have been invalidated.
418      </p>
419<p>
420        For the <code class="computeroutput">boost::asio::buffer</code> overloads that accept an argument
421        of type std::vector, the buffer objects returned are invalidated by any vector
422        operation that also invalidates all references, pointers and iterators referring
423        to the elements in the sequence (C++ Std, 23.2.4)
424      </p>
425<p>
426        For the <code class="computeroutput">boost::asio::buffer</code> overloads that accept an argument
427        of type std::basic_string, the buffer objects returned are invalidated according
428        to the rules defined for invalidation of references, pointers and iterators
429        referring to elements of the sequence (C++ Std, 21.3).
430      </p>
431<h5>
432<a name="boost_asio.reference.buffer.h3"></a>
433        <span class="phrase"><a name="boost_asio.reference.buffer.buffer_arithmetic"></a></span><a class="link" href="buffer.html#boost_asio.reference.buffer.buffer_arithmetic">Buffer
434        Arithmetic</a>
435      </h5>
436<p>
437        Buffer objects may be manipulated using simple arithmetic in a safe way which
438        helps prevent buffer overruns. Consider an array initialised as follows:
439      </p>
440<pre class="programlisting">boost::array&lt;char, 6&gt; a = { 'a', 'b', 'c', 'd', 'e' };
441</pre>
442<p>
443        A buffer object <code class="computeroutput">b1</code> created using:
444      </p>
445<pre class="programlisting">b1 = boost::asio::buffer(a);
446</pre>
447<p>
448        represents the entire array, <code class="computeroutput">{ 'a', 'b', 'c', 'd', 'e' }</code>. An
449        optional second argument to the <code class="computeroutput">boost::asio::buffer</code> function
450        may be used to limit the size, in bytes, of the buffer:
451      </p>
452<pre class="programlisting">b2 = boost::asio::buffer(a, 3);
453</pre>
454<p>
455        such that <code class="computeroutput">b2</code> represents the data <code class="computeroutput">{ 'a', 'b', 'c' }</code>.
456        Even if the size argument exceeds the actual size of the array, the size
457        of the buffer object created will be limited to the array size.
458      </p>
459<p>
460        An offset may be applied to an existing buffer to create a new one:
461      </p>
462<pre class="programlisting">b3 = b1 + 2;
463</pre>
464<p>
465        where <code class="computeroutput">b3</code> will set to represent <code class="computeroutput">{ 'c', 'd', 'e' }</code>.
466        If the offset exceeds the size of the existing buffer, the newly created
467        buffer will be empty.
468      </p>
469<p>
470        Both an offset and size may be specified to create a buffer that corresponds
471        to a specific range of bytes within an existing buffer:
472      </p>
473<pre class="programlisting">b4 = boost::asio::buffer(b1 + 1, 3);
474</pre>
475<p>
476        so that <code class="computeroutput">b4</code> will refer to the bytes <code class="computeroutput">{ 'b', 'c', 'd' }</code>.
477      </p>
478<h5>
479<a name="boost_asio.reference.buffer.h4"></a>
480        <span class="phrase"><a name="boost_asio.reference.buffer.buffers_and_scatter_gather_i_o"></a></span><a class="link" href="buffer.html#boost_asio.reference.buffer.buffers_and_scatter_gather_i_o">Buffers
481        and Scatter-Gather I/O</a>
482      </h5>
483<p>
484        To read or write using multiple buffers (i.e. scatter-gather I/O), multiple
485        buffer objects may be assigned into a container that supports the MutableBufferSequence
486        (for read) or ConstBufferSequence (for write) concepts:
487      </p>
488<pre class="programlisting">char d1[128];
489std::vector&lt;char&gt; d2(128);
490boost::array&lt;char, 128&gt; d3;
491
492boost::array&lt;mutable_buffer, 3&gt; bufs1 = {
493  boost::asio::buffer(d1),
494  boost::asio::buffer(d2),
495  boost::asio::buffer(d3) };
496bytes_transferred = sock.receive(bufs1);
497
498std::vector&lt;const_buffer&gt; bufs2;
499bufs2.push_back(boost::asio::buffer(d1));
500bufs2.push_back(boost::asio::buffer(d2));
501bufs2.push_back(boost::asio::buffer(d3));
502bytes_transferred = sock.send(bufs2);
503</pre>
504<h5>
505<a name="boost_asio.reference.buffer.h5"></a>
506        <span class="phrase"><a name="boost_asio.reference.buffer.requirements"></a></span><a class="link" href="buffer.html#boost_asio.reference.buffer.requirements">Requirements</a>
507      </h5>
508<p>
509        <span class="emphasis"><em>Header: </em></span><code class="literal">boost/asio/buffer.hpp</code>
510      </p>
511<p>
512        <span class="emphasis"><em>Convenience header: </em></span><code class="literal">boost/asio.hpp</code>
513      </p>
514</div>
515<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
516<td align="left"></td>
517<td align="right"><div class="copyright-footer">Copyright © 2003-2020 Christopher M.
518      Kohlhoff<p>
519        Distributed under the Boost Software License, Version 1.0. (See accompanying
520        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
521      </p>
522</div></td>
523</tr></table>
524<hr>
525<div class="spirit-nav">
526<a accesskey="p" href="bind_executor/overload2.html"><img src="../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../reference.html"><img src="../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../boost_asio.html"><img src="../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="buffer/overload1.html"><img src="../../../../doc/src/images/next.png" alt="Next"></a>
527</div>
528</body>
529</html>
530