Greg Dracoulis
Greg Dracoulis

Reputation: 3

Segfault working with vectors of type bool

I'm trying to solve a programming puzzle and running up against some difficulty. It's similar to Project Euler problem 215 but with blocks of width 3 and 4.5. Anyway, I initially approached it by brute forcing the combinations in C, but am trying to speed up the runtime by just calculating all the combinations in the first row and seeing how many valid ways there are to combine them and then going from there. I figured it'd be easier to do this working with vectors of booleans (tried bitsets but I can't use them since I don't have the width available at compile-time), but I'm not that experienced working with vectors and I've done something to make the segfault gods angry. I just can't see where.

I'm getting segmentation fault 11 when I feed the program arguments, so it's definitely something I did, and when I run a backtrace in GDB I get the following:

#0  0x0000000100002645 in std::_Bit_reference::operator= ()
#1  0x0000000100001be2 in build ()
#2  0x0000000100002287 in main ()

I know there's just got to be something I'm not seeing. It only happens when build() actually gets called, but I'm including main() just in case I might have done something wrong with the call.

#include <vector>

void build(std::vector<std::vector<bool> > possibilities, std::vector<bool> current, float width)
{
if(current.size() > 0)
{
    if(current.size() > width) return; // If we went over the specified width, bail out-invalid

    if (current.size() == width) // If we just matched the width for this row, push it on to our vector of possibilities
    {
        possibilities.push_back(current);
        return;
    }
}

// Try adding a block of length 3 and a block of length 4.5
std::vector<bool> branch1;
std::vector<bool> branch2;
if(current.size() > 0)
{
    branch1.assign( current.begin(), current.end() );
    branch2.assign( current.begin(), current.end() );

    branch1[ current.size() + 5 ] = 1;
    branch2[ current.size() + 8 ] = 1;
}
else
{
    branch1[5] = 1;
    branch2[8] = 1;
}
// Split off and check both branches
build(possibilities, branch1, width);
build(possibilities, branch2, width);
}

int main( int argc, char *argv[] )
{
if ( argc == 3 ) // Number of arguments should be 3-the program name, plus our width and height
{
    float width = (atof(argv[1]) * 2); // Width is assumed to be entered first, converting to integer
    int height = atoi(argv[2]); // The second argument should be height, ditto above
    if ( (width < 3) || (height < 1) ) // Catches non-number inputs (atof/i returns 0) and invalid entries
    {
        printf("Expected two numeric arguments, width and height, in that order.");
    }
    else // Continue the program
    {
        std::vector<bool> noo;
        std::vector<std::vector<bool> > possibilities;
        build(possibilities, noo, width);
        printf("%llu", (unsigned long long)possibilities.size());
    }
}
else
{
    printf("Expected two numeric arguments, width and height, in that order.");
}
}

Upvotes: 0

Views: 1525

Answers (1)

mfontanini
mfontanini

Reputation: 21900

Your noo vector:

std::vector<bool> noo;

Which is the second argument of build:

build(possibilities, noo, width);

Is empty. However, inside build, you perform some actions based on the size of that vector:

std::vector<bool> branch1;
std::vector<bool> branch2;
if(current.size() > 0) //current is actually noo
{
    branch1.assign( current.begin(), current.end() );
    branch2.assign( current.begin(), current.end() );

    branch1[ current.size() + 5 ] = 1;
    branch2[ current.size() + 8 ] = 1;
}
else
{
    branch1[5] = 1;
    branch2[8] = 1;
}

Since it is empty, you'll be accessing positions 5 and 8 of vectors branch1 and branch2(which are also empty), leading to undefined behaviour.

You should somehow resize branch1 and branch2 so that you don't perform an out-of-bounds access.

This way:

std::vector<bool> branch1(someNumber);

Will do it, but you should have a look at your code's logic, there surely is something else wrong. Moreover, you're passing arguments by value, so you're making unnecessary copies, and you won't see the modifications made to the possibilities vector from main.

Upvotes: 3

Related Questions