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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:57:53+00:00 2026-05-27T18:57:53+00:00

I have a problem with a destructor of struct Heap. Even just adding one,

  • 0

I have a problem with a destructor of struct Heap. Even just adding one, and not using it, creates a runtime exception (memory access). It’s second day I try to do it and tomorrow is a deadline.

struct Heap
{
       int n;
       int* tab;
       int* numerWKopcu;

       Heap () { n=0; }
       Heap (int size)  { this->tab = new int[liczbaDomow]; n=0; this->numerWKopcu = new int[2000100];}
       int  max()   { return tab[1]; }
       bool empty() { return n==0; }

       bool insert(int x)
       {
            n++;
            tab[n]=x;
            this->numerWKopcu[x] = n;//ZMIANA
            upHeap(n);
            return true;
       }         

       bool delMin()
       {
            if (n<1) return false;
            this->numerWKopcu[tab[n]] = 1; //ZMIANA
            tab[1]=tab[n]; n--;
            downHeap(1);
            return true;
       }

    void upHeap(int x){ 
        int p;
        int mem = tab[x];
        while (x>1)
        {
            p=x/2;
            if (color[mem]>color[tab[p]]) break;
            this->numerWKopcu[tab[p]] = x; //ZMIANA
            tab[x]=tab[p];
            x=p;
        }
        this->numerWKopcu[mem] = x;//ZMIANA
        tab[x]=mem;
    }

    void downHeap (int x)
    {
        int s=2*x;
        int mem=tab[x];
        while(s<=n)
        {
            if (s+1<=n && color[tab[s]]>color[tab[s+1]])
                s++;
            if (color[mem]>color[tab[s]])
            {
                this->numerWKopcu[tab[s]] = x; //ZMIANA
                tab[x]=tab[s];
                x=s;
                s=2*x;
            }
            else break;
        }
        this->numerWKopcu[mem] = x;//ZMIANA
        tab[x]=mem;
    }

    void write ()
    {
        for (int i=1;i<=n;i++) printf ("%d) %d\n", i, tab[i]);
        printf ("\n");
    }       

    void build()
    {
        int s = n;
        for (s=n/2; s>=1; s--) downHeap(s);
    }
    / ~Heap() {
          delete []this->numerWKopcu;
          delete []this-> tab; 
            }; 
}; 
  • 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-05-27T18:57:54+00:00Added an answer on May 27, 2026 at 6:57 pm

    The code is a bit hard to read, but I see two problems:

    • You aren’t initialising the pointers to null in the default constructor, so destroying a default-constructed object gives undefined behaviour;
    • You don’t define or remove the copy constructor and copy assignment operator (as you should always do if you define a destructor, per the Rule of Three), so destroying a copied object gives undefined behaviour.

    It’s also possible that you’re accessing memory outside the array bounds; a memory debugging tool such as valgrind can help you determine whether that’s happening.

    The simplest solution is to replace your manually-managed arrays with std::vector; then you won’t need to worry about writing your own destructors or copy semantics. You can also use at() rather than [] (at least in a debug variant) to give range-checked access.

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

Sidebar

Related Questions

I have problem creating new instance of excel 2007 using VBA (from Access 2002).
I faced a problem using a Boost variant. I have a segmentation fault when
Hallo, I have a quite strange problem in one of my C++ projects: I
I have an interesting problem in one of my C++ programs. Apperantly the vtable
I'm getting a weird problem in memory deallocation. I have the following code for
I have problem in some JavaScript that I am writing where the Switch statement
I have problem with return statment >.< I want to store all magazine names
I have problem with starting processes in impersonated context in ASP.NET 2.0. I am
I have problem compilin this code..can anyone tell whats wrong with the syntax CREATE
I have problem with ActionLink. I'd like to pass to my ActionLink parameter for

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.