MKSnazzy
MKSnazzy

Reputation: 81

Does each element in a vector have a unique memory address?

Fairly new to C++. Referring to this snippet of code:

for (std::vector<int>::iterator it = myvector.begin() ; it != myvector.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';

In particular when initializing the iterator and the end condition. Does each element in the vector have a unique memory address to point to? Or do iterators work differently than pointers?

Upvotes: 3

Views: 1198

Answers (4)

amanuel2
amanuel2

Reputation: 4646

Each element does indeed have a unique address in memory. Basically, let's look at this sample vector:

template <class T>
class vec
{
public:
 vec() ;
 ~vec() ;
 T * elements;
} 

You can see here is just an example of a vector. So what the iterators do is point to each pointer in the element. They may not have a unique address themselves, but they point to each element in the vector. So each iterator has UNIQUE memory addresses.

And like I said in my comments, they can't work differently since the Vectors and their iterators are libraries made by people like you, using C++. They can't work differently from pointers which are built in the C++ language.

Also from references: (draft of C++0x):

23.2.6 Class template vector [vector]

1 A vector is a sequence container that supports random access iterators. In addition, it supports (amortized) constant time insert and erase operations at the end; insert and erase in the middle take linear time. Storage management is handled automatically, though hints can be given to improve efficiency. The elements of a vector are stored contiguously, meaning that if v is a vector<T, Allocator> where T is some type other than bool, then it obeys the identity &v[n] == &v[0] + n for all 0 <= n < v.size().

Upvotes: 2

Benjamin Lindley
Benjamin Lindley

Reputation: 103733

Every object (that exists at a particular time) has a unique address. The elements of a vector are objects (with the exception of the abomination which is the std::vector<bool> specialization). So yes, they all have unique addresses. Keep in mind though, that when you add elements to a vector, it may reallocate. And when it reallocates, all the old objects are destroyed and moved to new objects with different addresses, and the old memory is free to be reused, perhaps by further reallocations of the same vector. The following is possible, for example:

std::vector<int> v;
v.push_back(0);
std::intptr_t a0 = (std::intptr_t)&v[0];

v.push_back(1); // a reallocation may happen here
v.push_back(2); // and perhaps another one here

std::intptr_t a2 = (std::intptr_t)&v[2];

if (a0 == a2) {
    std::cout << "This code is unlikely to be executed, but technically possible.";
}

Upvotes: 2

NathanOliver
NathanOliver

Reputation: 180845

From [vector.overview]/1

A vector is a sequence container that supports random access iterators. In addition, it supports (amortized) constant time insert and erase operations at the end; insert and erase in the middle take linear time. Storage management is handled automatically, though hints can be given to improve efficiency. The elements of a vector are stored contiguously, meaning that if v is a vector<T, Allocator> where T is some type other than bool, then it obeys the identity &v[n] == &v[0] + n for all 0 <= n < v.size().

Emphasis mine

So since &v[n] is valid for for all n in [0, size()) then ever element in the vector has its own unique address.

Upvotes: 1

vsoftco
vsoftco

Reputation: 56567

Although iterators are not necessarily pointers (although for std::vectors most non-debug implementations implement them as thin wrappers around pointers), std::vector is guaranteed to have its elements contiguous in memory. Which implies that YES, every element has a distinct memory location.

Upvotes: 6

Related Questions