CKTIS / C++
`std::vector::data`: buffer pointer and range bounds
Get a pointer to a vector’s elements, pass it with the element count, and know when the pointer is no longer valid.
Version: ISO C++23; implementation support may vary
Syntax
#include <vector>
// Member declarations of std::vector<T, Allocator> (T != bool):
constexpr T* data() noexcept;
constexpr const T* data() const noexcept;std::vector::data, declared in <vector>, returns a raw pointer into the vector’s contiguous storage in constant time, without copying or transferring ownership. For a non-empty vector, it returns the address of the first element. A non-const vector returns T*, which lets you modify existing elements; a const vector returns const T*. You cannot assign a new value to the element itself through const T*, but if T is a class type, you can still modify the element’s mutable members through that pointer. Both overloads are noexcept and have been constexpr since C++20. The std::vector<bool> specialization has no standard data() member.
If p = v.data() and n = v.size(), [p, p + n) is a valid half-open range. For a non-empty vector, p + n points one past the last element; never dereference it. When n == 0, p may or may not be nullptr, but it is not dereferenceable either way. Test v.empty() or n == 0, not whether the pointer is null.
Pass a buffer receiver the pointer and element count, or a pair of pointers delimiting the range. size() counts elements, not bytes; for char, the two counts coincide. A std::vector<char> buffer has no automatic \0 terminator, and an embedded \0 does not end a length-bounded read. For an empty buffer, check that the receiver accepts a zero count and a possibly null pointer. reserve() alone does not create elements: use resize() before writing to additional elements.
Do not free the storage through the pointer or use it after the buffer is destroyed. Reallocation invalidates element pointers and the old range endpoint. push_back() may reallocate; even if it does not, a pointer to an existing first element remains valid but the endpoint must be recomputed. After structural changes to the vector, the simplest approach is to get data() and size() again.
Examples
#include <cstddef>
#include <iostream>
#include <vector>
void print_bytes(const char* first, std::size_t count) {
const char* last = first + count;
for (auto p = first; p != last; ++p) {
std::cout << (*p == '\0' ? '0' : *p) << ' ';
}
std::cout << '\n';
}
int main() {
std::vector<char> bytes{'A', '\0'};
bytes.push_back('B');
const auto& input = bytes;
print_bytes(input.data(), input.size());
}
Result
A 0 B
`print_bytes` computes the one-past-end pointer and processes all three bytes. It displays the embedded `\0` as a visible `0`, and `<<` inserts a space after every character, including `B`, before the newline.
Related lessons
- C++ · Lesson 27 · Advanced · Version: 3
- C++ · Lesson 58 · Advanced · Version: 3
Sources
- Draft C++ Standard — Data [vector.data]
- Draft C++ Standard — Class template vector overview [vector.overview]
- Draft C++ Standard — Capacity [vector.capacity]
- Draft C++ Standard — Modifiers [vector.modifiers]
- Draft C++ Standard — Specialization of vector for bool [vector.bool]
- Draft C++ Standard — Additive operators [expr.add]
- Draft C++ Standard — Character inserter function templates [ostream.inserters.character]
- WG21 SG10 — SD-6: Feature-Test Macros and Policies