Replace Pointer-and-Size Buffer Parameters with C++20 std::span
Use C++20 std::span to bundle pointer and size into one non-owning view, avoiding mismatched buffer arguments.
14 Sept 2025, 20:34 UTC

The bug hiding in your buffer API
You maintain a function that parses a binary buffer:
void parse_header(const std::uint8_t* data, std::size_t length);
Every caller must pass a pointer and a length separately. Nothing stops a caller from passing packet.data() from one buffer and other_packet.size() from another. Inside the function, length is just a number; the type system cannot prove it matches data.
C++20 offers a replacement: std::span<T>, a non-owning view that carries a pointer and a size together. A span does not copy the elements it refers to, and it does not extend the lifetime of the underlying buffer.
What std::span actually is
Defined in <span>, std::span<T> is a view over a contiguous sequence of objects—an array, a std::vector, a std::array, or a raw pointer paired with a size. Because it is non-owning, copying a span copies only the pointer and length, not the data.
Standard-library containers can convert to a span implicitly, so a function that accepts std::span can be called with a std::vector directly.
Worked example: parsing a packet header
Here is the raw-pointer version:
#include <cstdint>
#include <cstddef>
void parse_header(const std::uint8_t* data, std::size_t length) {
if (length < 4) {
return;
}
std::uint32_t magic =
static_cast(data[0]) |
(static_cast(data[1]) << 8) |
(static_cast(data[2]) << 16) |
(static_cast(data[3]) << 24);
// ...
}
A caller manages two arguments:
std::vector packet = receive_packet();
parse_header(packet.data(), packet.size());
Here is the same function using std::span:
#include <span>
#include <cstdint>
void parse_header(std::span buffer) {
if (buffer.size() < 4) {
return;
}
std::uint32_t magic =
static_cast(buffer[0]) |
(static_cast(buffer[1]) << 8) |
(static_cast(buffer[2]) << 16) |
(static_cast(buffer[3]) << 24);
// ...
}
The call site drops the size argument entirely:
std::vector packet = receive_packet();
parse_header(packet);
You can also slice a span without allocating. first(n) returns the first n elements, last(n) returns the last n, and subspan(offset, count) returns a range starting at offset:
auto header = buffer.first(4);
auto payload = buffer.subspan(4);
Limits you must check yourself
std::span is not bounds-checked by default. operator[] performs no validation, so reading past the end is undefined behavior—the compiler will not insert a guard, and the type system will not stop you.
Because a span does not own its data, storing one beyond the lifetime of its source creates a dangling reference. This is safe:
void process(std::span data) {
// data is valid for the duration of this call
}
This is not:
std::span dangling;
{
std::vector v = {1, 2, 3};
dangling = v; // v is destroyed at the closing brace
}
// dangling now refers to freed memory
Element types are invariant: std::span<Derived> does not implicitly convert to std::span<Base>. If you pass polymorphic buffers, you will need an explicit cast or a templated helper.
Verify your toolchain before adopting it
std::span requires C++20. Check support with the feature-test macro __cpp_lib_span:
#ifdef __cpp_lib_span
static_assert(__cpp_lib_span >= 202002L, "std::span is required");
#endif
Compile a minimal program from a terminal (no special permissions required):
g++ -std=c++20 -Wall -Wextra example.cpp -o example
Replace example.cpp with your source file. On MSVC, use /std:c++20 instead of -std=c++20. Expected check: the program compiles without errors about span or <span>. Risk: AddressSanitizer will not automatically diagnose a span that outlives its vector, because the span type itself does not track ownership.
When to use it
Use std::span<T> for function parameters that read a contiguous buffer and do not need to store the view after the call returns. It makes the buffer length visible at the call site and eliminates mismatched pointer/size pairs. Avoid it as a class member that may outlive the source container, and do not use it when the function needs to resize or own the buffer—std::vector or std::string is a better fit there.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.