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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:08:32+00:00 2026-05-18T01:08:32+00:00

I’m building a hashmap class that can have string keys and ints, bools, strings

  • 0

I’m building a hashmap class that can have string keys and ints, bools, strings or pointers of different types as its values, and I want it to work. For the program I’m using it for I create the pointer and pass it into the hashmap. The problem comes when I need to destruct the map. If the type for the hashmap is a pointer I need to delete it(the value) before I delete it’s container.

so the code I have right now goes something like this:
I have a hashNode** drawers, which I use as a two dimensional array to hold pointer to hashNodes in the map. Those same pointers are also held in another hashNode** array, which stores them as they are added to map (for ease/speed of growing and copying the hashmap).

template <typename V>
class str_map {
public:
    // ...
    virtual ~str_map() {
        str_map<V>::~str_map();
    }
    // ....
};

and then later I have a bunch of methods like these:
one for regular values:

template <>
str_map<int>::~str_map() {
    for(int i=0; i < count && array[i] != NULL; i++){
        delete array[i];
    }
    delete array;
    delete drawers;
}

and one for pointers:

template <>
str_map<str_map<int>*>::~str_map() {
    for(int i=0; i < count && array[i]->val() != NULL; i++)
        delete array[i]->val();
    for(int i=0; i < count && array[i] != NULL; i++){
        delete array[i];
    }
    delete array;
    delete drawers;
}

Is there another better way to deconstruct an instance of str_map class correctly so that all the memory is handled correctly? Or at least a way to make this work?

  • 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-18T01:08:33+00:00Added an answer on May 18, 2026 at 1:08 am

    Your container should handle values. That’s it, no more, no less. If someone wants to stick pointers in, let them. Don’t take ownership of whatever they may or may not be pointing at.

    It’s up to the users of your hash map to know how to manage the lifetime of their memory. They should be using smart pointers, so your class just copies them around and the smart pointer manages the memory.

    The guideline is manage one resource, or none at all. If you are managing more than one resource, you’ve set yourself up for failure.

    I suspect delete array should be delete [] array;. What this means is you really should be using std::vector. Again, either manage one resource or none at all. std::vector manages one resource, so you don’t have to. And so on.

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

Sidebar

Related Questions

I have a bunch of posts stored in text files formatted in yaml/textile (from
I am trying to loop through a bunch of documents I have to put
I have some data like this: 1 2 3 4 5 9 2 6
I'm making a simple page using Google Maps API 3. My first. One marker

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.