buffer.html 25.6 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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>buffer</title>
<link rel="stylesheet" href="../../boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.75.2">
<link rel="home" href="../../index.html" title="Asio">
<link rel="up" href="../reference.html" title="Reference">
<link rel="prev" href="bind_executor/overload2.html" title="bind_executor (2 of 2 overloads)">
<link rel="next" href="buffer/overload1.html" title="buffer (1 of 32 overloads)">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr><td valign="top"><img alt="asio C++ library" width="250" height="60" src="../../asio.png"></td></tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="bind_executor/overload2.html"><img src="../../prev.png" alt="Prev"></a><a accesskey="u" href="../reference.html"><img src="../../up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../home.png" alt="Home"></a><a accesskey="n" href="buffer/overload1.html"><img src="../../next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="asio.reference.buffer"></a><a class="link" href="buffer.html" title="buffer">buffer</a>
</h3></div></div></div>
<p>
        <a class="indexterm" name="asio.indexterm.buffer"></a> 
The <code class="computeroutput">asio::buffer</code> function is used
        to create a buffer object to represent raw memory, an array of POD elements,
        a vector of POD elements, or a std::string.
      </p>
<p>
        Create a new modifiable buffer from an existing buffer.
      </p>
<pre class="programlisting">mutable_buffer <a class="link" href="buffer/overload1.html" title="buffer (1 of 32 overloads)">buffer</a>(
    const mutable_buffer &amp; b);
  <span class="emphasis"><em>» <a class="link" href="buffer/overload1.html" title="buffer (1 of 32 overloads)">more...</a></em></span>

mutable_buffer <a class="link" href="buffer/overload2.html" title="buffer (2 of 32 overloads)">buffer</a>(
    const mutable_buffer &amp; b,
    std::size_t max_size_in_bytes);
  <span class="emphasis"><em>» <a class="link" href="buffer/overload2.html" title="buffer (2 of 32 overloads)">more...</a></em></span>
</pre>
<p>
        Create a new non-modifiable buffer from an existing buffer.
      </p>
<pre class="programlisting">const_buffer <a class="link" href="buffer/overload3.html" title="buffer (3 of 32 overloads)">buffer</a>(
    const const_buffer &amp; b);
  <span class="emphasis"><em>» <a class="link" href="buffer/overload3.html" title="buffer (3 of 32 overloads)">more...</a></em></span>

const_buffer <a class="link" href="buffer/overload4.html" title="buffer (4 of 32 overloads)">buffer</a>(
    const const_buffer &amp; b,
    std::size_t max_size_in_bytes);
  <span class="emphasis"><em>» <a class="link" href="buffer/overload4.html" title="buffer (4 of 32 overloads)">more...</a></em></span>
</pre>
<p>
        Create a new modifiable buffer that represents the given memory range.
      </p>
<pre class="programlisting">mutable_buffer <a class="link" href="buffer/overload5.html" title="buffer (5 of 32 overloads)">buffer</a>(
    void * data,
    std::size_t size_in_bytes);
  <span class="emphasis"><em>» <a class="link" href="buffer/overload5.html" title="buffer (5 of 32 overloads)">more...</a></em></span>
</pre>
<p>
        Create a new non-modifiable buffer that represents the given memory range.
      </p>
<pre class="programlisting">const_buffer <a class="link" href="buffer/overload6.html" title="buffer (6 of 32 overloads)">buffer</a>(
    const void * data,
    std::size_t size_in_bytes);
  <span class="emphasis"><em>» <a class="link" href="buffer/overload6.html" title="buffer (6 of 32 overloads)">more...</a></em></span>
</pre>
<p>
        Create a new modifiable buffer that represents the given POD array.
      </p>
<pre class="programlisting">template&lt;
    typename PodType,
    std::size_t N&gt;
mutable_buffer <a class="link" href="buffer/overload7.html" title="buffer (7 of 32 overloads)">buffer</a>(
    PodType (&amp;data)[N]);
  <span class="emphasis"><em>» <a class="link" href="buffer/overload7.html" title="buffer (7 of 32 overloads)">more...</a></em></span>

template&lt;
    typename PodType,
    std::size_t N&gt;
mutable_buffer <a class="link" href="buffer/overload8.html" title="buffer (8 of 32 overloads)">buffer</a>(
    PodType (&amp;data)[N],
    std::size_t max_size_in_bytes);
  <span class="emphasis"><em>» <a class="link" href="buffer/overload8.html" title="buffer (8 of 32 overloads)">more...</a></em></span>
</pre>
<p>
        Create a new non-modifiable buffer that represents the given POD array.
      </p>
<pre class="programlisting">template&lt;
    typename PodType,
    std::size_t N&gt;
const_buffer <a class="link" href="buffer/overload9.html" title="buffer (9 of 32 overloads)">buffer</a>(
    const PodType (&amp;data)[N]);
  <span class="emphasis"><em>» <a class="link" href="buffer/overload9.html" title="buffer (9 of 32 overloads)">more...</a></em></span>

template&lt;
    typename PodType,
    std::size_t N&gt;
const_buffer <a class="link" href="buffer/overload10.html" title="buffer (10 of 32 overloads)">buffer</a>(
    const PodType (&amp;data)[N],
    std::size_t max_size_in_bytes);
  <span class="emphasis"><em>» <a class="link" href="buffer/overload10.html" title="buffer (10 of 32 overloads)">more...</a></em></span>
</pre>
<p>
        Create a new modifiable buffer that represents the given POD array.
      </p>
<pre class="programlisting">template&lt;
    typename PodType,
    std::size_t N&gt;
mutable_buffer <a class="link" href="buffer/overload11.html" title="buffer (11 of 32 overloads)">buffer</a>(
    boost::array&lt; PodType, N &gt; &amp; data);
  <span class="emphasis"><em>» <a class="link" href="buffer/overload11.html" title="buffer (11 of 32 overloads)">more...</a></em></span>

template&lt;
    typename PodType,
    std::size_t N&gt;
mutable_buffer <a class="link" href="buffer/overload12.html" title="buffer (12 of 32 overloads)">buffer</a>(
    boost::array&lt; PodType, N &gt; &amp; data,
    std::size_t max_size_in_bytes);
  <span class="emphasis"><em>» <a class="link" href="buffer/overload12.html" title="buffer (12 of 32 overloads)">more...</a></em></span>
</pre>
<p>
        Create a new non-modifiable buffer that represents the given POD array.
      </p>
<pre class="programlisting">template&lt;
    typename PodType,
    std::size_t N&gt;
const_buffer <a class="link" href="buffer/overload13.html" title="buffer (13 of 32 overloads)">buffer</a>(
    boost::array&lt; const PodType, N &gt; &amp; data);
  <span class="emphasis"><em>» <a class="link" href="buffer/overload13.html" title="buffer (13 of 32 overloads)">more...</a></em></span>

template&lt;
    typename PodType,
    std::size_t N&gt;
const_buffer <a class="link" href="buffer/overload14.html" title="buffer (14 of 32 overloads)">buffer</a>(
    boost::array&lt; const PodType, N &gt; &amp; data,
    std::size_t max_size_in_bytes);
  <span class="emphasis"><em>» <a class="link" href="buffer/overload14.html" title="buffer (14 of 32 overloads)">more...</a></em></span>

template&lt;
    typename PodType,
    std::size_t N&gt;
const_buffer <a class="link" href="buffer/overload15.html" title="buffer (15 of 32 overloads)">buffer</a>(
    const boost::array&lt; PodType, N &gt; &amp; data);
  <span class="emphasis"><em>» <a class="link" href="buffer/overload15.html" title="buffer (15 of 32 overloads)">more...</a></em></span>

template&lt;
    typename PodType,
    std::size_t N&gt;
const_buffer <a class="link" href="buffer/overload16.html" title="buffer (16 of 32 overloads)">buffer</a>(
    const boost::array&lt; PodType, N &gt; &amp; data,
    std::size_t max_size_in_bytes);
  <span class="emphasis"><em>» <a class="link" href="buffer/overload16.html" title="buffer (16 of 32 overloads)">more...</a></em></span>
</pre>
<p>
        Create a new modifiable buffer that represents the given POD array.
      </p>
<pre class="programlisting">template&lt;
    typename PodType,
    std::size_t N&gt;
mutable_buffer <a class="link" href="buffer/overload17.html" title="buffer (17 of 32 overloads)">buffer</a>(
    std::array&lt; PodType, N &gt; &amp; data);
  <span class="emphasis"><em>» <a class="link" href="buffer/overload17.html" title="buffer (17 of 32 overloads)">more...</a></em></span>

template&lt;
    typename PodType,
    std::size_t N&gt;
mutable_buffer <a class="link" href="buffer/overload18.html" title="buffer (18 of 32 overloads)">buffer</a>(
    std::array&lt; PodType, N &gt; &amp; data,
    std::size_t max_size_in_bytes);
  <span class="emphasis"><em>» <a class="link" href="buffer/overload18.html" title="buffer (18 of 32 overloads)">more...</a></em></span>
</pre>
<p>
        Create a new non-modifiable buffer that represents the given POD array.
      </p>
<pre class="programlisting">template&lt;
    typename PodType,
    std::size_t N&gt;
const_buffer <a class="link" href="buffer/overload19.html" title="buffer (19 of 32 overloads)">buffer</a>(
    std::array&lt; const PodType, N &gt; &amp; data);
  <span class="emphasis"><em>» <a class="link" href="buffer/overload19.html" title="buffer (19 of 32 overloads)">more...</a></em></span>

template&lt;
    typename PodType,
    std::size_t N&gt;
const_buffer <a class="link" href="buffer/overload20.html" title="buffer (20 of 32 overloads)">buffer</a>(
    std::array&lt; const PodType, N &gt; &amp; data,
    std::size_t max_size_in_bytes);
  <span class="emphasis"><em>» <a class="link" href="buffer/overload20.html" title="buffer (20 of 32 overloads)">more...</a></em></span>

template&lt;
    typename PodType,
    std::size_t N&gt;
const_buffer <a class="link" href="buffer/overload21.html" title="buffer (21 of 32 overloads)">buffer</a>(
    const std::array&lt; PodType, N &gt; &amp; data);
  <span class="emphasis"><em>» <a class="link" href="buffer/overload21.html" title="buffer (21 of 32 overloads)">more...</a></em></span>

template&lt;
    typename PodType,
    std::size_t N&gt;
const_buffer <a class="link" href="buffer/overload22.html" title="buffer (22 of 32 overloads)">buffer</a>(
    const std::array&lt; PodType, N &gt; &amp; data,
    std::size_t max_size_in_bytes);
  <span class="emphasis"><em>» <a class="link" href="buffer/overload22.html" title="buffer (22 of 32 overloads)">more...</a></em></span>
</pre>
<p>
        Create a new modifiable buffer that represents the given POD vector.
      </p>
<pre class="programlisting">template&lt;
    typename PodType,
    typename Allocator&gt;
mutable_buffer <a class="link" href="buffer/overload23.html" title="buffer (23 of 32 overloads)">buffer</a>(
    std::vector&lt; PodType, Allocator &gt; &amp; data);
  <span class="emphasis"><em>» <a class="link" href="buffer/overload23.html" title="buffer (23 of 32 overloads)">more...</a></em></span>

template&lt;
    typename PodType,
    typename Allocator&gt;
mutable_buffer <a class="link" href="buffer/overload24.html" title="buffer (24 of 32 overloads)">buffer</a>(
    std::vector&lt; PodType, Allocator &gt; &amp; data,
    std::size_t max_size_in_bytes);
  <span class="emphasis"><em>» <a class="link" href="buffer/overload24.html" title="buffer (24 of 32 overloads)">more...</a></em></span>
</pre>
<p>
        Create a new non-modifiable buffer that represents the given POD vector.
      </p>
<pre class="programlisting">template&lt;
    typename PodType,
    typename Allocator&gt;
const_buffer <a class="link" href="buffer/overload25.html" title="buffer (25 of 32 overloads)">buffer</a>(
    const std::vector&lt; PodType, Allocator &gt; &amp; data);
  <span class="emphasis"><em>» <a class="link" href="buffer/overload25.html" title="buffer (25 of 32 overloads)">more...</a></em></span>

template&lt;
    typename PodType,
    typename Allocator&gt;
const_buffer <a class="link" href="buffer/overload26.html" title="buffer (26 of 32 overloads)">buffer</a>(
    const std::vector&lt; PodType, Allocator &gt; &amp; data,
    std::size_t max_size_in_bytes);
  <span class="emphasis"><em>» <a class="link" href="buffer/overload26.html" title="buffer (26 of 32 overloads)">more...</a></em></span>
</pre>
<p>
        Create a new modifiable buffer that represents the given string.
      </p>
<pre class="programlisting">template&lt;
    typename Elem,
    typename Traits,
    typename Allocator&gt;
mutable_buffer <a class="link" href="buffer/overload27.html" title="buffer (27 of 32 overloads)">buffer</a>(
    std::basic_string&lt; Elem, Traits, Allocator &gt; &amp; data);
  <span class="emphasis"><em>» <a class="link" href="buffer/overload27.html" title="buffer (27 of 32 overloads)">more...</a></em></span>

template&lt;
    typename Elem,
    typename Traits,
    typename Allocator&gt;
mutable_buffer <a class="link" href="buffer/overload28.html" title="buffer (28 of 32 overloads)">buffer</a>(
    std::basic_string&lt; Elem, Traits, Allocator &gt; &amp; data,
    std::size_t max_size_in_bytes);
  <span class="emphasis"><em>» <a class="link" href="buffer/overload28.html" title="buffer (28 of 32 overloads)">more...</a></em></span>
</pre>
<p>
        Create a new non-modifiable buffer that represents the given string.
      </p>
<pre class="programlisting">template&lt;
    typename Elem,
    typename Traits,
    typename Allocator&gt;
const_buffer <a class="link" href="buffer/overload29.html" title="buffer (29 of 32 overloads)">buffer</a>(
    const std::basic_string&lt; Elem, Traits, Allocator &gt; &amp; data);
  <span class="emphasis"><em>» <a class="link" href="buffer/overload29.html" title="buffer (29 of 32 overloads)">more...</a></em></span>

template&lt;
    typename Elem,
    typename Traits,
    typename Allocator&gt;
const_buffer <a class="link" href="buffer/overload30.html" title="buffer (30 of 32 overloads)">buffer</a>(
    const std::basic_string&lt; Elem, Traits, Allocator &gt; &amp; data,
    std::size_t max_size_in_bytes);
  <span class="emphasis"><em>» <a class="link" href="buffer/overload30.html" title="buffer (30 of 32 overloads)">more...</a></em></span>
</pre>
<p>
        Create a new modifiable buffer that represents the given string_view.
      </p>
<pre class="programlisting">template&lt;
    typename Elem,
    typename Traits&gt;
const_buffer <a class="link" href="buffer/overload31.html" title="buffer (31 of 32 overloads)">buffer</a>(
    basic_string_view&lt; Elem, Traits &gt; data);
  <span class="emphasis"><em>» <a class="link" href="buffer/overload31.html" title="buffer (31 of 32 overloads)">more...</a></em></span>
</pre>
<p>
        Create a new non-modifiable buffer that represents the given string.
      </p>
<pre class="programlisting">template&lt;
    typename Elem,
    typename Traits&gt;
const_buffer <a class="link" href="buffer/overload32.html" title="buffer (32 of 32 overloads)">buffer</a>(
    basic_string_view&lt; Elem, Traits &gt; data,
    std::size_t max_size_in_bytes);
  <span class="emphasis"><em>» <a class="link" href="buffer/overload32.html" title="buffer (32 of 32 overloads)">more...</a></em></span>
</pre>
<p>
        A buffer object represents a contiguous region of memory as a 2-tuple consisting
        of a pointer and size in bytes. A tuple of the form <code class="computeroutput">{void*, size_t}</code>
        specifies a mutable (modifiable) region of memory. Similarly, a tuple of
        the form <code class="computeroutput">{const void*, size_t}</code> specifies a const (non-modifiable)
        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>
        and <a class="link" href="const_buffer.html" title="const_buffer"><code class="computeroutput">const_buffer</code></a>,
        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>
        is implicitly convertible to a <a class="link" href="const_buffer.html" title="const_buffer"><code class="computeroutput">const_buffer</code></a>,
        and the opposite conversion is not permitted.
      </p>
<p>
        The simplest use case involves reading or writing a single buffer of a specified
        size:
      </p>
<pre class="programlisting">sock.send(asio::buffer(data, size));
</pre>
<p>
        In the above example, the return value of <code class="computeroutput">asio::buffer</code> meets
        the requirements of the ConstBufferSequence concept so that it may be directly
        passed to the socket's write function. A buffer created for modifiable memory
        also meets the requirements of the MutableBufferSequence concept.
      </p>
<p>
        An individual buffer may be created from a builtin array, std::vector, std::array
        or boost::array of POD elements. This helps prevent buffer overruns by automatically
        determining the size of the buffer:
      </p>
<pre class="programlisting">char d1[128];
size_t bytes_transferred = sock.receive(asio::buffer(d1));

std::vector&lt;char&gt; d2(128);
bytes_transferred = sock.receive(asio::buffer(d2));

std::array&lt;char, 128&gt; d3;
bytes_transferred = sock.receive(asio::buffer(d3));

boost::array&lt;char, 128&gt; d4;
bytes_transferred = sock.receive(asio::buffer(d4));
</pre>
<p>
        In all three cases above, the buffers created are exactly 128 bytes long.
        Note that a vector is <span class="emphasis"><em>never</em></span> automatically resized when
        creating or using a buffer. The buffer size is determined using the vector's
        <code class="computeroutput">size()</code> member function, and not its capacity.
      </p>
<h5>
<a name="asio.reference.buffer.h0"></a>
        <span><a name="asio.reference.buffer.accessing_buffer_contents"></a></span><a class="link" href="buffer.html#asio.reference.buffer.accessing_buffer_contents">Accessing
        Buffer Contents</a>
      </h5>
<p>
        The contents of a buffer may be accessed using the <code class="computeroutput">data()</code> and
        <code class="computeroutput">size()</code> member functions:
      </p>
<pre class="programlisting">asio::mutable_buffer b1 = ...;
std::size_t s1 = b1.size();
unsigned char* p1 = static_cast&lt;unsigned char*&gt;(b1.data());

asio::const_buffer b2 = ...;
std::size_t s2 = b2.size();
const void* p2 = b2.data();
</pre>
<p>
        The <code class="computeroutput">data()</code> member function permits violations of type safety,
        so uses of it in application code should be carefully considered.
      </p>
<p>
        For convenience, a <a class="link" href="buffer_size.html" title="buffer_size"><code class="computeroutput">buffer_size</code></a>
        function is provided that works with both buffers and buffer sequences (that
        is, types meeting the ConstBufferSequence or MutableBufferSequence type requirements).
        In this case, the function returns the total size of all buffers in the sequence.
      </p>
<h5>
<a name="asio.reference.buffer.h1"></a>
        <span><a name="asio.reference.buffer.buffer_copying"></a></span><a class="link" href="buffer.html#asio.reference.buffer.buffer_copying">Buffer
        Copying</a>
      </h5>
<p>
        The <a class="link" href="buffer_copy.html" title="buffer_copy"><code class="computeroutput">buffer_copy</code></a>
        function may be used to copy raw bytes between individual buffers and buffer
        sequences.
      </p>
<p>
        In particular, when used with the <a class="link" href="buffer_size.html" title="buffer_size"><code class="computeroutput">buffer_size</code></a>
        function, the <a class="link" href="buffer_copy.html" title="buffer_copy"><code class="computeroutput">buffer_copy</code></a>
        function can be used to linearise a sequence of buffers. For example:
      </p>
<pre class="programlisting">vector&lt;const_buffer&gt; buffers = ...;

vector&lt;unsigned char&gt; data(asio::buffer_size(buffers));
asio::buffer_copy(asio::buffer(data), buffers);
</pre>
<p>
        Note that <a class="link" href="buffer_copy.html" title="buffer_copy"><code class="computeroutput">buffer_copy</code></a>
        is implemented in terms of <code class="computeroutput">memcpy</code>, and consequently it cannot
        be used to copy between overlapping memory regions.
      </p>
<h5>
<a name="asio.reference.buffer.h2"></a>
        <span><a name="asio.reference.buffer.buffer_invalidation"></a></span><a class="link" href="buffer.html#asio.reference.buffer.buffer_invalidation">Buffer
        Invalidation</a>
      </h5>
<p>
        A buffer object does not have any ownership of the memory it refers to. It
        is the responsibility of the application to ensure the memory region remains
        valid until it is no longer required for an I/O operation. When the memory
        is no longer available, the buffer is said to have been invalidated.
      </p>
<p>
        For the <code class="computeroutput">asio::buffer</code> overloads that accept an argument of type
        std::vector, the buffer objects returned are invalidated by any vector operation
        that also invalidates all references, pointers and iterators referring to
        the elements in the sequence (C++ Std, 23.2.4)
      </p>
<p>
        For the <code class="computeroutput">asio::buffer</code> overloads that accept an argument of type
        std::basic_string, the buffer objects returned are invalidated according
        to the rules defined for invalidation of references, pointers and iterators
        referring to elements of the sequence (C++ Std, 21.3).
      </p>
<h5>
<a name="asio.reference.buffer.h3"></a>
        <span><a name="asio.reference.buffer.buffer_arithmetic"></a></span><a class="link" href="buffer.html#asio.reference.buffer.buffer_arithmetic">Buffer
        Arithmetic</a>
      </h5>
<p>
        Buffer objects may be manipulated using simple arithmetic in a safe way which
        helps prevent buffer overruns. Consider an array initialised as follows:
      </p>
<pre class="programlisting">boost::array&lt;char, 6&gt; a = { 'a', 'b', 'c', 'd', 'e' };
</pre>
<p>
        A buffer object <code class="computeroutput">b1</code> created using:
      </p>
<pre class="programlisting">b1 = asio::buffer(a);
</pre>
<p>
        represents the entire array, <code class="computeroutput">{ 'a', 'b', 'c', 'd', 'e' }</code>. An
        optional second argument to the <code class="computeroutput">asio::buffer</code> function may be
        used to limit the size, in bytes, of the buffer:
      </p>
<pre class="programlisting">b2 = asio::buffer(a, 3);
</pre>
<p>
        such that <code class="computeroutput">b2</code> represents the data <code class="computeroutput">{ 'a', 'b', 'c' }</code>.
        Even if the size argument exceeds the actual size of the array, the size
        of the buffer object created will be limited to the array size.
      </p>
<p>
        An offset may be applied to an existing buffer to create a new one:
      </p>
<pre class="programlisting">b3 = b1 + 2;
</pre>
<p>
        where <code class="computeroutput">b3</code> will set to represent <code class="computeroutput">{ 'c', 'd', 'e' }</code>.
        If the offset exceeds the size of the existing buffer, the newly created
        buffer will be empty.
      </p>
<p>
        Both an offset and size may be specified to create a buffer that corresponds
        to a specific range of bytes within an existing buffer:
      </p>
<pre class="programlisting">b4 = asio::buffer(b1 + 1, 3);
</pre>
<p>
        so that <code class="computeroutput">b4</code> will refer to the bytes <code class="computeroutput">{ 'b', 'c', 'd' }</code>.
      </p>
<h5>
<a name="asio.reference.buffer.h4"></a>
        <span><a name="asio.reference.buffer.buffers_and_scatter_gather_i_o"></a></span><a class="link" href="buffer.html#asio.reference.buffer.buffers_and_scatter_gather_i_o">Buffers and
        Scatter-Gather I/O</a>
      </h5>
<p>
        To read or write using multiple buffers (i.e. scatter-gather I/O), multiple
        buffer objects may be assigned into a container that supports the MutableBufferSequence
        (for read) or ConstBufferSequence (for write) concepts:
      </p>
<pre class="programlisting">char d1[128];
std::vector&lt;char&gt; d2(128);
boost::array&lt;char, 128&gt; d3;

boost::array&lt;mutable_buffer, 3&gt; bufs1 = {
  asio::buffer(d1),
  asio::buffer(d2),
  asio::buffer(d3) };
bytes_transferred = sock.receive(bufs1);

std::vector&lt;const_buffer&gt; bufs2;
bufs2.push_back(asio::buffer(d1));
bufs2.push_back(asio::buffer(d2));
bufs2.push_back(asio::buffer(d3));
bytes_transferred = sock.send(bufs2);
</pre>
<h5>
<a name="asio.reference.buffer.h5"></a>
        <span><a name="asio.reference.buffer.requirements"></a></span><a class="link" href="buffer.html#asio.reference.buffer.requirements">Requirements</a>
      </h5>
<p>
        <span class="emphasis"><em>Header: </em></span><code class="literal">asio/buffer.hpp</code>
      </p>
<p>
        <span class="emphasis"><em>Convenience header: </em></span><code class="literal">asio.hpp</code>
      </p>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2003-2020 Christopher M.
      Kohlhoff<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        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>)
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="bind_executor/overload2.html"><img src="../../prev.png" alt="Prev"></a><a accesskey="u" href="../reference.html"><img src="../../up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../home.png" alt="Home"></a><a accesskey="n" href="buffer/overload1.html"><img src="../../next.png" alt="Next"></a>
</div>
</body>
</html>