0

I want to run a simple program in C++ in which there are two vectors - one is a std::vector<int> and the other is a std::vector<bool> of equal length. The value of the boolean vector at an index decides whether the value of the integer vector will be printed or not. Here is a copy of the program I am trying to run:

#include<bits/stdc++.h>
using namespace std;

int main(){
    vector<int> arr{1, 2, 3, 4, 5};
    vector<bool> b(true, arr.size());

    for(int i=0; i<arr.size(); i++){
        if(b[i])
            cout<<arr[i]<<endl;
    }
    return 0;
}

The above program runs as expected. But there is an Address Boundary Error encountered when I change the values from true to false in the std::vector initialization line. Precisely, the error is:

fish: Job 1, './test_vector_bool' terminated by signal SIGSEGV (Address boundary error)

I am already aware of some of the major pitfalls of using vector<bool> from these threads:

What I gathered from these threads is we should not be using reference for vector<bool> in cpp. But all I need to know is why is the vector with true working and initialization with false failing?

1 Answers1

3

The line vector<bool> b(true, arr.size()); will create a vector, with 1 element, of value arr.size(). Thus, the loop will get out of bounds.

It's crucial to remember that when we construct a container this way, we say: "How much of what".

In this case: "We need arr.size() elements with value true":

vector<bool> b(arr.size(), true);

For more information, you may want to check the documentation on vector constructors, the third one in particular, "fill constructor".

rawrex
  • 4,044
  • 2
  • 8
  • 24
  • I feel so stupid to have messed this up. Once in a *fool* moon, we do forget basic things. I came back to delete this question, but since you took the pain to answer this stupid question I am leaving it as it is marking your answer as solution. – Deeptendu Santra Jul 01 '21 at 09:53
  • @DeeptenduSantra happens to the best of us! It's all good, you should feel free to manage the post as you consider it should be managed. Best of luck to you! – rawrex Jul 01 '21 at 09:56
  • I completely missed the fact because there should be some out of bounds error when I run the code in the question, the result is 1 2 3 4 5. It should return boundary error in that case too. – Deeptendu Santra Jul 01 '21 at 10:07
  • You might want to explain the (not shown) line `vector b(false, arr.size());` instead of (or in addition to) `vector b(true, arr.size());`, as the former is what triggered the SIGSEGV signal. – JaMiT Jul 02 '21 at 02:47
  • @rawrex *"You should feel free to manage the post as you consider it should be managed."* -- nice sentiment, but unfortunately not realistic. A user cannot delete their own question if it has an answer with upvotes. – JaMiT Jul 02 '21 at 02:50
  • @JaMiT *"A user cannot delete their own question if it has an answer with upvotes"* - was not aware of that. Thanks! Will consider taking actions based on the fresh input shortly. – rawrex Jul 02 '21 at 02:52