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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T09:35:09+00:00 2026-06-10T09:35:09+00:00

I dont seem to be able to find the source of the error. The

  • 0

I dont seem to be able to find the source of the error. The code defines a struct called boolfunc. I define a function to add two boolfunc(s). I get the error: segmentation fault (core dumped). I know what it means but where is the source of the error.

#include<iostream>
#include<vector>
using namespace std;

class boolfunc {
public:
  int num_var;
  int num_terms;
  vector<vector<int> > func;
  boolfunc addfunc(boolfunc f1, boolfunc f2);
};

boolfunc addfunc(boolfunc f1, boolfunc f2)
  {
    boolfunc f3;
    f3.func.resize(0);
    f3.num_terms = f1.num_terms + f2.num_terms;
    int var_dif = f1.num_var - f2.num_var;
    if(var_dif >= 0)
        {
        f3.num_var=f1.num_var;
        f3.func.resize(f1.num_var);
        f2.num_var=f1.num_var;
            f2.func.resize(f1.num_var);
        for(int i=0; i<f2.num_terms; ++i)
            {
            for(int j=0; j<var_dif; ++j)
                {
                f2.func[i].push_back(-1);
                }
            }
        }
    else
        {
        f3.num_var=f2.num_var;
        f3.func.resize(f2.num_var);
        f1.num_var=f2.num_var;
        f1.func.resize(f2.num_var);
        for(int i=0; i<f1.num_terms; ++i)
            {
            for(int j=0; j<(-1)*(var_dif); ++j)
                {
                f1.func[i].push_back(-1);
                }
            }
        }
    for(int i=0; i<f1.num_terms; ++i)
        {
        f3.func[i]=f1.func[i];
        }   
    for(int j=0; j<f2.num_terms;++j)
        {
        f3.func[j+f1.num_terms]=f2.func[j];
        }
    return f3;
    }

int main()
{
boolfunc ef1, ef2, ef3;
ef1.func.resize(0);
ef2.func.resize(0);
ef3.func.resize(0);
cout<< "Input the number of variables of function 1" <<endl;
cin>> ef1.num_var;
cout<< "Input the number of variables of function 2" <<endl;
cin>> ef2.num_var;
cout<< "Input the number of terms of function 1" <<endl;
cin>> ef1.num_terms;
cout<< "Input the number of terms of function 2" <<endl;
cin>> ef2.num_terms;
ef1.func.resize(ef1.num_terms);
  for(int i=0; i<ef1.num_terms; ++i)
    {
    ef1.func[i].resize(ef1.num_var);
    for(int j=0; j<ef1.num_var; ++j)
        {
    cout<<"For function 1, Input the term "<<i+1<<"'s variable "<<j+1<<"'s     value:";
        cin>>ef1.func[i][j];
        }
    }
ef2.func.resize(ef2.num_terms);
  for(int i=0; i<ef2.num_terms; ++i)
    {
    ef2.func[i].resize(ef2.num_var);
    for(int j=0; j<ef2.num_var; ++j)
        {
    cout<<"For function 2, Input the term "<<i+1<<"'s variable "<<j+1<<"'s     value:";
        cin>>ef2.func[i][j];
        }
    }
ef3 = addfunc(ef1, ef2);
for(int i=0; i<ef3.num_terms; ++i)
    {
    for(int j=0; j<ef3.num_var; ++j)
        {
        cout<<ef3.func[i][j]<<' ';
        if(j==ef3.num_var -1) cout<<endl;
        }
    }
return 0;
}
  • 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-10T09:35:11+00:00Added an answer on June 10, 2026 at 9:35 am

    How to approach this:

    Build and run

    g++ -Wall -Wextra -O0 -ggdb main.cpp
    gdb --args ./a.out
    

    With the example vector (0,0,1,0) your program fails at line 49:

    f3.func[i]=f1.func[i];
    

    With the error:

    Input the number of variables of function 1
    0
    Input the number of variables of function 2
    0
    Input the number of terms of function 1
    1
    Input the number of terms of function 2
    0
    
    Program received signal SIGSEGV, Segmentation fault.
    0x0000000000402930 in std::vector<int, std::allocator<int> >::capacity (this=0x0) at /usr/include/c++/4.6/bits/stl_vector.h:652
    652              - this->_M_impl._M_start); }
    (gdb) bt
    #0  0x0000000000402930 in std::vector<int, std::allocator<int> >::capacity (this=0x0) at /usr/include/c++/4.6/bits/stl_vector.h:652
    #1  0x0000000000401bf3 in std::vector<int, std::allocator<int> >::operator= (this=0x0, __x=...) at /usr/include/c++/4.6/bits/vector.tcc:164
    #2  0x0000000000400fc3 in addfunc (f1=..., f2=...) at main.cpp:49
    #3  0x0000000000401558 in main () at main.cpp:92
    

    Analyse

    If you look at the boundary where your program ends and the standard library starts, we immediately see the bug:

    std::vector<int, std::allocator<int> >::operator= (this=0x0, __x=...) at /usr/include/c++/4.6/bits/vector.tcc:164
    

    As you can see NULL is passed as this argument to std::vector<..>::operator= which means your f3.func vector has the wrong size.

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

Sidebar

Related Questions

I dont seem to be able to find any evidence of multiple groupby being
I have a question that I dont seem to be able to find an
i dont seem to be able to figure out how to add participants (guests)
I've been googling this question a lot and I dont seem to find a
I'm getting a syntax error that I don't seem to find an answer to
Is there a destructor for Java? I don't seem to be able to find
I don't seem to be able to find any authoritative, up-to-date (i.e. for 1.0
I don't seem to be able to use a custom route with pagination. The
I've stumbled upon a problem I don't seem to be able to solve on
I've been reading about this on StackOverflow and facebook doc, yet I dont seem

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.