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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:34:21+00:00 2026-05-23T02:34:21+00:00

I’m implementing a binary heap class. The heap is implemented as an array that

  • 0

I’m implementing a binary heap class. The heap is implemented as an array that is dynamically allocated. The heap class has members capacity, size and a pointer to an array, as:

class Heap
{
    private:
       Heap* H;
       int capacity; //Size of the array.
       int size; //Number of elements currently in the array
       ElementType* Elements; //Pointer to the array of size (capacity+1)

       //I've omitted the rest of the class.
};

My construcor looks like this:

Heap::Heap (int maxElements)
{
    H = ( Heap* ) malloc ( sizeof ( Heap ) );
    H -> Elements = ( ElementType* ) malloc ( ( maxElements+1 )*sizeof ( ElementType ) );
    H -> Elements[0] = DUMMY_VALUE; //Dummy value
    H -> capacity = maxElements;
    H -> size = 0;  
}

Since I’m mallocing twice and dereferencing both pointers in the constructor, I should check whether it succeeds. But what should I do if it fails? The constructor can’t itself return anything to indicate that it failed. Is it good programming practice to avoid mallocs in constructors altogether?

  • 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-23T02:34:22+00:00Added an answer on May 23, 2026 at 2:34 am

    First of all, you do not need a Heap* member variable inside your Heap object, and you certainly should not be allocating memory for it in the Heap constructor – that’s just asking for trouble. Nor should you be accessing your member variables as H->Elements, but rather simply as Elements.

    The only thing you need to allocate is the Elements array.

    With regards to handling allocation failures, constructors should indicate failures via an exception. There is even a standard exception type, std::bad_alloc that is usually used to indicate a failure to allocate memory.

    For example:

    #include <stdexcept>  // for std::bad_alloc
    ...
    Heap::Heap (int maxElements) 
    {
        Elements = ( ElementType* ) malloc ( ( maxElements+1 )*sizeof ( ElementType ) );
        if (Elements == NULL)
            throw std::bad_alloc("Failed to allocate memory");
        ...
    }
    

    Even better, use new rather than malloc to allocate the memory. new will automatically throw an exception of type std::bad_alloc if it fails to allocate memory.

    Example:

    Heap::Heap (int maxElements) 
    {
        Elements = new ElementType[maxElements + 1];  // throws std::bad_alloc on failure
        ...
    }
    

    Note: if you use new to allocate the object, you must use delete to free it rather than free. (Correction: In the example above, you are using the array form of new, new[], so you should call the array form of delete, delete[]).

    Finally, you haven’t shown how ElementType is declared, but if it’s a type that has a non-default constructor/destructor (or if it’s a template parameter which means it can potentially be such a type), you have to use new rather than malloc when allocating it because malloc will not call the constructor (and free will not call the destructor). In general, it’s good practice to just always use new and delete in C++ rather than malloc and free.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
Basically, what I'm trying to create is a page of div tags, each has
I have a French site that I want to parse, but am running into
I have a reasonable size flat file database of text documents mostly saved in
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post

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.