Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8136131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T10:39:32+00:00 2026-06-06T10:39:32+00:00

I’m trying to solve a programming puzzle and running up against some difficulty. It’s

  • 0

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.");
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T10:39:35+00:00Added an answer on June 6, 2026 at 10:39 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I am currently running into a problem where an element is coming back from
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.