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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T12:16:36+00:00 2026-06-09T12:16:36+00:00

I wrote this hash map (this was a part of telephonic interview exercise), where

  • 0

I wrote this hash map (this was a part of telephonic interview exercise), where I do a new Node(key, value) when I put an element. I want to make sure I’m cleaning up when the hashmap itself goes out of scope.

Did I miss anything in here ? Is there any way I can check if there is a memory leak ?

class HashMap {
private:
    list<Node*> data[SIZE];

public:
    ~HashMap();
    Node* get(int key);
    void put(int key, int value);

    int hashFn(int val){ return val % 13; }
};

HashMap::~HashMap(){
    for(int i = 0; i < SIZE; ++i){
        list<Node*>& val = data[i];
        for(list<Node*>::iterator it = val.begin(); it != val.end(); it++){
            Node* n = *it;
            delete n;
        }
    }
}

For the curios: complete code is here: http://rextester.com/EHPCYW12862

EDIT:

Also, do I really need to call list.clear() in the end (since I’ve already deallocated all the nodes in a list) ?

  • 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-09T12:16:37+00:00Added an answer on June 9, 2026 at 12:16 pm

    It seems put is constructing a Node to put into your hash table, associating the key and value. There was no need to use a list<Node *>, it would have been cleaner to use list<Node> instead.

    list<Node> data[SIZE];
    //...
    data[bucket].push_front(Node(key, value));
    

    Then, you could have avoided implementing a destructor.

    Your get function can still return a pointer.

    Node* HashMap::get(int key){
        //...
        list<Node>::iterator it = data[bucket].begin();
        //...
                if (it->key == key) return &*it;
        //...
        return NULL;
    }
    

    If you leave the implementation with list<Node *>, then you should also implement a copy constructor and an assignment operator (the rule of three).

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

Sidebar

Related Questions

let say I have this code Map<String, String> list = new HashMap<String, String>(); list.put(number1,
I wrote this as a simple dice game. It works as I want except
I wrote this program: #include <stdio.h> /*Part B Write a program that: defines an
I wrote this function to make columns sortable. I want to rearrange divs based
I wrote this n-array tree class. I want to write a method to add
I am new to Java Multithreading World, I wrote this program, I just wanted
I wrote this code for zoom in/out . it works but even with one
I wrote this code for zoom in / out and it suppesed to only
I wrote this short program which has a tiny GUI. Its supposed to allow
I wrote this infeasible oml by mistake while trying to do another thing. What's

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.