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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T23:09:19+00:00 2026-06-14T23:09:19+00:00

When I call the default constructor Heap() in int main(), I have a feeling

  • 0

When I call the default constructor Heap() in int main(), I have a feeling that I am not initializing my vector pointer properly giving me a segmentation fault. I am unsure of how to fix this issue. Below is a sample code with comments around the areas that I am having trouble (segmentation faults when adding elements to the vector using push_back).

#include <vector>
#include <iostream>

struct A
{
  A(int av, int bv):a(av),b(bv){}

  int a, b;
};

struct Heap
{
  Heap() : ptr() {}

  Heap(std::vector<A> *p) : ptr(p) {}

  void makeHeap()
  {
    heapLoc.push_back(0);
    if(ptr->size()==1){
      return;
    }

    for(int i=1; i<ptr->size(); ++i){
      heapLoc.push_back(i);
      filterUp(i);
    }
  }

  void filterUp(int idx)
  {
    int parent;
    parent = (idx-1)/2;

    while((*ptr)[heapLoc[parent]].b > (*ptr)[heapLoc[idx]].b){
      std::swap(heapLoc[parent],heapLoc[idx]);
      idx=parent;
      parent=(parent-1)/2;
    }
  }

  void pushHeap(A newA)
  {
    int idx;

    ptr->push_back(newA);  //SEGMENTATION FAULT caused by this line when calling h2.pushback(a8)
    idx=ptr->size()-1;
    heapLoc.push_back(idx);
    filterUp(idx);
  }

  std::vector<A> *ptr;
  std::vector<int> heapLoc;
};


int main()
{
  A a0(2,5), a1(4,2), a2(8,4), a3(0,3), a4(2,1),a5(2,7);  
  std::vector<A> aArray;  
  aArray.push_back(a0);
  aArray.push_back(a1);
  aArray.push_back(a2); 
  aArray.push_back(a3);
  aArray.push_back(a4);
  aArray.push_back(a5);

  Heap h(&aArray);
  h.makeHeap();

  A a8(99,1);
  h.pushHeap(a8);  //works fine

  Heap h2();
  h2.pushHeap(a8); //SEGMENTATION FAULT

  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-14T23:09:21+00:00Added an answer on June 14, 2026 at 11:09 pm

    I guess your default constructor should look something like this:

    Heap() : ptr(new std::vector<A> ()) {}
    

    In your current variant your ptr is initialized with some undefined value (NULL or perhaps some trash). It should be initialized with a pointer at an actual vector.

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

Sidebar

Related Questions

Is there any way to tell db4o to call class default constructor while activating
I have a set of classes where my base has a constructor that takes
I have a base class that declares and defines a constructor, but for some
Does JVM call default Constructor of Anonymous Class to create instance. My understanding may
In C#, if I have an inherited class with a default constructor, do I
I have parameterized constructor for the master page and no default constructor. How can
If I have a user defined default constructor in my Test class, and what
I have a util class that is supposed to call a method on a
I'm trying to call explicit constructor/destructor with traits in templatized function. template <int i>
I am trying to call to make a 2-arg constructor the default constructor. By

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.