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

  • Home
  • SEARCH
  • 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 8889849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:21:46+00:00 2026-06-14T22:21:46+00:00

I am trying to write a C++ code for a minimum Heap. I want

  • 0

I am trying to write a C++ code for a minimum Heap. I want to create a vector of pointers and make sure that they are properly deleated.

I am able to create a vector of pointers, however I get an invalid conversion error from the default constructor. Why is this the case?

Also, I am trying to write a user defined destructor to ensure that I don’t have any memory issues. However, I can’t figure out why I am getting an error that the pointer was not allocated.

#include <vector>
#include <iostream>

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

struct Heap
{
   Heap() : ptr(new std::vector<A*>()) {}  //WHY AM I GETTING AN ERROR FOR THE DEFAULT CONSTRUCTOR AND NOT THE CONSTRUCTOR BELOW?
   //ERROR: invalid conversion from ‘std::vector<A*, std::allocator<A*> >*’ to ‘long unsigned int’
   //ERROR: initializing argument 1 of ‘std::vector<_Tp, _Alloc>::vector(size_t, const _Tp&, const _Alloc&) [with _Tp = A*, _Alloc = std::allocator<A*>]’

   Heap(std::vector<A*> p) : ptr(p) {  //Works fine. 
       makeHeap();
   }

  ~Heap(){   //I DON'T UNDERSTAND WHY I AM GETTING A MEMORY ERROR HERE
        std::vector<A*>::iterator it;
    for(it=ptr.begin(); it<ptr.end(); ++it)
    {  
      delete *it;
      *it=NULL;
    }
  }//ERROR:  malloc pointer being freed was not allocated  

  void makeHeap()
  {  //some code  }

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


int main()
{
  A a0(2,5), a1(4,2);  
  std::vector<A*> aArray;  
  aArray.push_back(&a0);
  aArray.push_back(&a1);

  Heap h(aArray);

  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-14T22:21:47+00:00Added an answer on June 14, 2026 at 10:21 pm

    ptr is a vector of pointers, not a pointer to a vector. So you can’t use new to construct it on the heap and the store the address.

    Instead, construct it like this:

    Heap() : ptr() {}
    

    This is call the default constructor for ptr, i.e. it will create an empty vector of pointers. You should also consider changing the name of ptr since it is not really a pointer.


    Regarding your destructor (Dietmar has meanwhile posted an answer, and before him Cameron a comment, that explains it (+1 for that), but for the sake of completeness): you are getting a memory error because the pointers you store in your vector refer to addresses of objects you created on the stack. Specifically, the objects you create here:

    int main()
    {
      A a0(2,5), a1(4,2);  // <-- automatic storage, so they
                           //     will be deallocated automatically
      /* ... */
    }
    

    If you want your Heap object to be responsible for its own objects, you should have your constructor create objects on the heap that the destructor can then delete. One way of doing this is to define the copy constructor of Heap as follows:

    Heap(const std::vector<A*> &p) : ptr() {
      std::vector<A*>::const_iterator it(p.begin());
      for ( ; it != p.end() ; ++it)
        ptr.push_back(new A(**it));
    }
    

    Your destructor can be used as-is then, but you may want to use != instead of < in the for-loop.

    ~Heap() {
       std::vector<A*>::iterator it;
       for(it=ptr.begin(); it != ptr.end(); ++it)
       {  
         delete *it;
         *it=NULL;
       }
    

    Finally, I am not sure if you really need a vector of pointers (rather than a vector of objects), but if you think you do, consider using smart pointers (e.g. C++11 offers std::unique_ptr, so you could define a std::vector<std::unique_ptr<A>> to get around many of the allocation related problems).

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

Sidebar

Related Questions

I'm trying to write code in my controller that when run, will create a
I m trying write code that after reset set up rrpmax as 3000. It
I'm trying to write code that will load an image from a resource, and
I've been trying to write code that loads a .png file, attaches hotspot information,
Trying to write a code at the moment that basically tests to see if
I'm trying to write a code that adds a class to a div for
I am trying to write some code that will generate accurate .proto files from
I'm new to lisp and trying to write a recursive function that returns minimum
I'm trying to write something that format Brazilian phone numbers, but I want it
I am trying to write code to search all children for a div that

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.