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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:32:24+00:00 2026-06-13T23:32:24+00:00

I have a problem about destructor. In below main.cpp, if I define ht as

  • 0

I have a problem about destructor. In below main.cpp, if I define ht as a pointer, the program works fine. But if I define ht as an object, it will throw an error

“malloc: * error for object 0x7fff511a4b00: pointer being freed was not allocated
*
set a breakpoint in malloc_error_break to debug”

I understand this error and my solution is to define ht as a pointer.

My question is whether I can define ht as an object directly or not, if the answer is yes, how should I modify the class definition.

Thanks.

main.cpp

#include "copy_constructor.h"

int main()
{
  // Define ht as pointer works fine
  //Hash_Table<int>  *ht;
  //ht = new Hash_Table<int>;

  Hash_Table<int> ht;

  // keyed_structure<int> s;
  //  s.data_val = 10;

  return 0;
 }

copy_constructor.h

#ifndef COPY_CONSTRUCTOR_H
#define COYP_CONSTRUCTOR_H

#include <cstdlib>
#include <iostream>

const size_t MAX_SIZE = 3;

template<class D> struct keyed_structure
{
  size_t key_val;
  D data_val;
  bool empty_val;
};

template<class D> class Hash_Table
{
  public:
    // COnstructor
    Hash_Table() {
      //h_table = new keyed_structure<D> [MAX_SIZE];
      for (size_t index=0; index < MAX_SIZE; ++index) {
        h_table[index].empty_val = true;
      }
    }
    // Destructor
    ~Hash_Table() {
      //std::cout << "Do destruct" << std::endl;
      delete[] h_table;
    }

  private:
     keyed_structure<D> h_table[MAX_SIZE];
};

#endif
  • 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-13T23:32:26+00:00Added an answer on June 13, 2026 at 11:32 pm

    You don’t need to call delete[] h_table; as you didn’t new anything in Hash_Table. Your code is undefined behavior.

    The reason your test doesn’t crash is because you only new ht but didn’t delete ht

      Hash_Table<int>  *ht;
      ht = new Hash_Table<int>;
      delete ht;  // if you did ht, it still crashes
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have problem adding arraylist to list view, will explain about my problem here..
I have problem about program to find the sum of 10 numbers in an
I have some problem about operator overloading. I looked everywhere but couldn't find a
I have a problem about casting a CallableStatement to OracleCallableStatement . It gives ClassCastException
I have a problem about div position relative alignment. I want the second div
I am using MFC CFile Seek function. I have a problem about Seek out
I have a little problem about the sort direction of a specific column in
I have a great problem about the rotation in three.js I want to rotate
I have a little problem about Google Maps. I'm using Geocoding (xml) for getting
I have a little problem about using jQuery (I really do not know jQuery

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.