Home C++ Vectors Functions data() C++ std::vector data() Function Definition In C++, std::vector::data() returns a pointer to the memory where the vector keeps its elements. Because a vector is contiguous , that pointer behaves like a plain C array of size() elements.
It is the safe modern form of &v[0]. Prefer data(), especially when the vector might be empty. Include <vector> to use it.
This is a member of std::vector<T>, a dynamic contiguous array . Elements sit in one block, so v[i] and data() are fast. Growing past capacity() reallocates and can invalidate iterators, references, and pointers. Prefer push_back / pop_back at the end; middle inserts shift elements.
Syntax Call data with empty brackets.
int* p = v.data();
const int* q = cv.data();
legacy_api(v.data(), v.size()); Call What it does v.data()Returns a T* to the first element (or a non-dereferenceable pointer if empty). v.data()[i]Same as v[i] when the vector is not empty and i is in range.
For an empty vector, data() may return nullptr or another non-dereferenceable pointer. Do not read or write through it. Always check empty() or pass size() to the other API.
Library declarations T* data() noexcept;
const T* data() const noexcept; Parameters Parameter Description None data() takes no parameters. Call it with empty brackets.
Return Value Return Value Description T* / const T*Pointer to the first element, or a non-dereferenceable pointer when size() is 0.
Important
Calling data() on an empty vector and then reading *p is undefined behavior. Also, a later reallocation (from push_back, insert, reserve, and so on) can invalidate the pointer.
C++ Examples 1. Basic pointer 2. C-style API 3. Empty vector Example 1 — Basic data() Get a pointer to the first element and print a few values through it.
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {10, 20, 30};
int* p = v.data();
std::cout << p[0] << ' ' << p[1] << ' ' << p[2] << '\n';
return 0;
} How It Works v.data() returns the address of the first element, 10.Because storage is contiguous, p[1] and p[2] are the next elements. This matches how a C array works, which is why data() is useful with older APIs. Example 2 — Pass data() to a C-style Function A helper takes a pointer and a count. Vector supplies both.
#include <iostream>
#include <vector>
int sum(const int* p, std::size_t n) {
int total = 0;
for (std::size_t i = 0; i < n; ++i) {
total += p[i];
}
return total;
}
int main() {
std::vector<int> v = {1, 2, 3, 4};
std::cout << "sum=" << sum(v.data(), v.size()) << '\n';
return 0;
} How It Works The function expects a C array pointer and a length. v.data() is the pointer and v.size() is the length.Together they describe the whole contiguous block without copying. Example 3 — Do Not Dereference data() When Empty Guard with empty() before reading through the pointer.
#include <iostream>
#include <vector>
void show_first(const std::vector<int>& v) {
if (v.empty()) {
std::cout << "empty\n";
return;
}
std::cout << "first=" << *v.data() << '\n';
}
int main() {
std::vector<int> a;
std::vector<int> b = {42};
show_first(a);
show_first(b);
return 0;
} How It Works For the empty vector, the function returns early. It never dereferences data(). For {42}, data() points at the only element, so printing *v.data() is safe. std::array is never empty in the same way; a vector can shrink to size 0, so this check matters.Frequently Asked Questions What does std::vector::data() return? A pointer to the first element of the contiguous storage, or a non-dereferenceable pointer if the vector is empty.
When should I use data()? Use it for C APIs that want a T* and a length. Prefer at(), [], or iterators in normal C++ code.
Is data() the same as &v[0]? When the vector is not empty, yes they point to the same place. data() is clearer and defined for empty vectors (but still must not be dereferenced).
Is data() the same as begin()? No. data() returns a raw pointer. begin() returns an iterator. They often address the same element, but the types differ.
Do deque and list have data()? std::deque and std::list are not one contiguous block for all elements, so they have no data(). std::array and std::vector do.
Next: emplace() Build a new element in place at any position (C++11) with std::vector.
emplace() tutorial → About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
10 people found this page helpful
Helpful Share Copy link Suggestion