Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do different vector<bool> elements share the same address?

Tags:

c++

I have defined the following class.

class STTreeNode
{
public:
    int ind;
    int parentInd;  
    std::vector<int> childInds;

    int numTrain;
    std::vector<bool> isInfluenced;

    STTreeNode(int ind, int parentInd, int numTrain);
};

STTreeNode::STTreeNode(int ind, int parentInd, int numTrain) {
    this->ind = ind;
    this->parentInd = parentInd;
    this->numTrain = numTrain;
}

and I ran the following code snippet.

STTreeNode *a = new STTreeNode(3, 4, 5);
a->childInds.push_back(20);
a->childInds.push_back(30);
a->isInfluenced.push_back(true);
a->isInfluenced.push_back(false);

for (int i = 0; i < a->childInds.size(); i++)
    std::cout << &(a->childInds[i]) << " ";
std::cout << std::endl;
for (int i = 0; i < a->isInfluenced.size(); i++)
    std::cout << &(a->isInfluenced[i]) << " ";
std::cout << std::endl;

The output was

0000020351C18520 0000020351C18524
00000083540FFC60 00000083540FFC60

I am very confused about this. Why do the two elements in a->childInds have consecutive addresses (as expected), while the two elements in a->isInfluenced seem to share the same address?

Update:

From the comments I understand that this has to do with the difference between vector<bool> and other vectors. Are there any other special cases about vector I should be aware of, or is vector<bool> the only one I need to watch out for?

like image 698
edwardliang2019 Avatar asked Sep 18 '19 10:09

edwardliang2019


People also ask

What is a bool vector?

The vector<bool> class is a partial specialization of vector for elements of type bool . It has an allocator for the underlying type that's used by the specialization, which provides space optimization by storing one bool value per bit.

Can vector have different data types?

Yes it is possible to hold two different types, you can create a vector of union types.

How do you check if all elements are same in a vector?

A simple solution to check if all elements of a vector are equal is using the std::adjacent_find function. It returns the first occurrence of adjacent elements that satisfies a binary predicate, or end of the range if no such pair is found.

Is linked list faster than vector?

For example, taking a bunch of random integers and inserting them in sorted order into a vector or a linked list -- the vector will always be faster, regardless of the number of items total, due to cache misses when searching for the insertion point in the linked list.


1 Answers

They don't. Well, they do…

vector<bool> is not like other vectors.

The elements of a vector<bool> cannot be directly accessed like this, because they are/may be smaller than a byte, which is the "resolution" of addresses in C++. So many bits/elements are packed into a single memory location on your computer. But that's not why you get these results.

You're sort of observing the address of a temporary. That temporary is some proxy object that provides mutable access to a single bit in your collection.

I say "sort of" because you're not even really taking the address; said proxy object has an operator& that gives you something called a "bit iterator" in some implementations.

Here's Howard Hinnant on vector<bool>.

In the meantime, when working with vector<bool>, unlearn what you know about vectors. 😜


Are there any other special cases about vector I should be aware of, or is vector<bool> the only one I need to watch out for?

vector<bool> is the only one.

like image 80
Lightness Races in Orbit Avatar answered Oct 05 '22 04:10

Lightness Races in Orbit