Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does vec.data() return if vec.size() == 0?

Tags:

c++

vector

cppreference has this note for std::vector::data:

Returns pointer to the underlying array serving as element storage. The pointer is such that range [data(); data() + size()) is always a valid range, even if the container is empty.

What does "valid range" mean here exactly? What will data() return if the vector is zero-length?

Specifically, for a zero-length vector:

  1. Can data() ever be a null pointer?
  2. Can it be safely dereferenced? (Even if it points to junk.)
  3. Is it guaranteed to be different between two different (zero-length) vectors?

I am working with a C library that takes arrays and won't allow a null pointer even for a zero-length array. However, it does not actually dereference the array storage pointer if the array length is zero, it just checks whether it is NULL. I want to make sure that I can safely pass data() to this C library, so the only relevant question is (1) above. (2) and (3) are just out of curiosity in case a similar situation comes up.


Update

Based on comments that were not turned into answers, we can try the following program:

#include <iostream> #include <vector>  using namespace std;  int main() {     vector<int> v;     cout << v.data() << endl;      v.push_back(1);     cout << v.data() << endl;      v.pop_back();     cout << v.data() << endl;      v.shrink_to_fit();     cout << v.data() << endl;      return 0; } 

With my compiler it output:

0x0 0x7f896b403300 0x7f896b403300 0x0 

This shows that:

  • data() can indeed be a null pointer, thus the answers are (1) yes (2) no (3) no

  • but it is not always a null pointer for a zero-size vector

Yes, obviously I should have tried this before asking.

like image 806
Szabolcs Avatar asked Mar 15 '16 09:03

Szabolcs


People also ask

What does vector data () do?

vector data() function in C++ STL The std::vector::data() is an STL in C++ which returns a direct pointer to the memory array used internally by the vector to store its owned elements. Parameters: The function does not accept any parameters.

What does vector member size () do?

vector::size() size() function is used to return the size of the vector container or the number of elements in the vector container.

How do I return a vector size in C++?

To get the size of a C++ Vector, you can use size() function on the vector. size() function returns the number of elements in the vector.

What is the size of empty vector in C++?

If the vector container is empty what will size() and empty() returns? Size will return 0 and empty will return 1 because if there is no element in the vector the size of that vector will be 0 and empty will be true so it will return 1.


1 Answers

"valid range" is defined by [iterator.requirements.general]/7 (C++14):

"Range [i,j) is valid if and only if j is reachable from i".

Luckily C++ defines that adding 0 to a null pointer yields a null pointer. So, is a null pointer reachable from a null pointer ? This is defined by point 6 of the same section:

An iterator j is called reachable from an iterator i if and only if there is a finite sequence of applications of the expression ++i that makes i == j.

A zero-length sequence is a finite sequence, therefore data() may return a null pointer.

Accordingly the answers to your questions are:

  1. Can data() ever be a null pointer?

Yes

  1. Can it be safely dereferenced? (Even if it points to junk.)

No

  1. Is it guaranteed to be different between two different (zero-length) vectors?

No

like image 138
M.M Avatar answered Oct 16 '22 04:10

M.M