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 7750433
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:17:26+00:00 2026-06-01T11:17:26+00:00

I am solving a 0-1knapsack problem. I solved the problem by brute force algorithm.

  • 0

I am solving a 0-1knapsack problem.
I solved the problem by brute force algorithm.

In main.cpp

  int main(int argc, char *argv[])
  {
     ......
     int solution;
     solution = bruteForce();
     ......
  }

The strange thing is, when I implement the bruteForce() in main.cpp, my program works correctly, however, after I move the bruteForce() to bruteForce.cpp and included it in main.cpp, the program will produce segmentation fault when calling the bruteForce().

Here’s how I move bruteForce() to bruteForce.cpp.
First I created a header functions.h(because I want to solve the problem by other method after implement brute force successfully)

  functions.h:

  #include "global.h"
  int bruteForce();
  int multiplication( int );

And then I move bruteForce() to bruteForce.cpp

  #include <iostream>
  #include <stdlib.h>
  #include <vector>
  #include "global.h"
  #include "functions.h"

  using namespace std;

  int bruteForce()
  {
     int bestValue = 0;
     int j, tempSize, tempValue;
     int bestChoice[n+1];

     for(int i=0; i<multiplication(n); i++)
     {
         tempSize = 0;
         tempValue =0;
         j = n;

         while(x[j]!=0 && j>0)
         {
             x[j] = 0;
             j--;
         }
         x[j] = 1;

         for(int k=1; k<=n; k++)
         {
             if(x[k] == 1)
             {
                 tempSize += size[k];
                 tempValue += value[k];
             }
         }


        if((tempValue > bestValue) && (tempSize <= S)) 
        {   
            bestValue = tempValue;
            for(int p=1; p<=n; p++)
                bestChoice[p] = x[p];
        }
    }
    for(int p=1; p<=n; p++)
       x[p] = bestChoice[p];

    return bestValue;
 }

In global.h, I declared some glabal variables:

  #include <vector>
  using std::vector;

  static int n, S;
  static vector<int> value, size, x;

The gdb debugger shows

  Program received signal SIGSEGV, Segmentation fault.
  0x08049308 in main()

Any idea about why this happens?

Thanks in advance.

Oh BTW, if you need more info, here’s the package.
You can first type make in the root of this package. Then type this to execute.

  ./bin/01knapsack -BF inputs/n5S11.in n5s11.out
  • 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-01T11:17:28+00:00Added an answer on June 1, 2026 at 11:17 am

    You shouldn’t put your variables in a header file. When you include that from both your source files, both will get their own individual copy of that variable – and thus you wont be able to transfer data between your functions in the way you think (or at least, that’s my understanding of how it should work – I’ll admit that I’m not 100% sure what actually happens).

    The best way to transfer data to a function is to use parameters though. Call the function with whatever it needs, and return data either through the function return value, or through a pointer or reference parameter. Using global variables for stuff like this is error prone (as you have seen), and it’s much less clear for others looking at your code.

    If you absolutely want to use global variables, declare them in one of your source files, and put them in your global header file with an extern statement in front of it. When you then include the header from another file, extern tells the compiler that it shouldn’t actually create the variable itself, but rather that it is provided by another object file.

    So, in main.cpp:

    int n, S;
    vector<int> value, size, x;
    

    And in global.h:

    extern int n, S;
    extern vector<int> value, size, x;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am solving problem 9 on the Project Euler . In my solution I
While solving a geometry problem, I came across an approach called Sliding Window Algorithm.
I'm solving some problem that involves Rabin–Karp string search algorithm. This algorithm requires rolling
I am solving this problem of rotating an array and got algorithm and code
I am solving the problem in the interview question about LinkedList. question is... Write
Working on solving a simple problem in C++, want to understand how I could
Consider this way of solving the Subset sum problem: def subset_summing_to_zero (activities): subsets =
I've been problem solving this for the past few hours and frustratingly can't find
Consider the 0/1 knapsack problem . The standard Dynamic Programming algorithm applies only when
While solving a DP related problem, I observed that first works but the second

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.