Home / Forums / std::span and std::string_view: Zero-Copy Memory Slices in Modern C++20

UnreliableCode Community

Developer Research, Reverse Engineering & Coding Community

Tutorial

std::span and std::string_view: Zero-Copy Memory Slices in Modern C++20

KronoDev
C++ / Game Modder
MEMBER
Rep: 195
Join Date: Jan 2023
Posts: 32
Thanks: 48
4y ago · Jan 1, 2022 10:00 AM
#1
How std::span<T> and std::string_view provide bounds-safe, non-owning views over contiguous memory buffers without heap allocation.
KronoDev - Keep coding, keep learning
The following users thanked KronoDev for this post:
VectorByte
Graphics & DirectX Dev
VIP
Rep: 420
Join Date: Aug 2022
Posts: 39
Thanks: 115
4y ago · Jan 1, 2022 11:27 AM
#2
std::span avoids pointer+length parameter pairs across functions. It is completely zero-cost at runtime with compile-time extent checks.
VectorByte | DirectX 11/12 Hooking & ImGui Overlays
Quote
Math is the language of game engines.
PEHeader
PE Format & Linker Tech
VIP
Rep: 350
Join Date: Jun 2023
Posts: 35
Thanks: 95
4y ago · Jan 3, 2022 2:27 PM
#3
Always remember that std::string_view is not guaranteed to be null-terminated, so avoid passing .data() directly to C APIs expecting null terminators!
PEHeader | IMAGE_NT_HEADERS & Section Parsing