-1

I got a global variable and a function that everytime it's called, it creates an object of type Myclass, and inserts a pointer to that object inside a HashTable.

So, I want that everytime 'insert' is called it creates a new object of Myclass, makes a pointer to it and store that pointer into a hash table.

Something like:

Myclass object;

void insert()
{
    object.doSomething();
    Myclass * ptr = &object;
    HashTable.insert(ptr);
}

But it doesn't work since everytime 'insert' is called it overwrite the previous object.

  • Recommendation: Don't store a pointer in the hash table. Store the object directly and the whole problem goes away. – user4581301 Nov 28 '18 at 23:19
  • I know, but we're asked to store a pointer :] – p4storr Nov 28 '18 at 23:20
  • Well that sucks. Can you store `std::unique_ptr`? It's still a pointer... Sorta. If not, `HashTable.insert(new Myclass);` and make sure to iterate through the hash table and `delete` all of the `Myclass`es it stores when you're done. Better still, embrace [RAII](https://stackoverflow.com/questions/2321511/what-is-meant-by-resource-acquisition-is-initialization-raii) and teach `HashTable` to own (build and destroy in this case) the contained `Myclass`es . – user4581301 Nov 28 '18 at 23:28

2 Answers2

0

You never create a new Myclass object. You're just getting the same pointer to the same object every time insert() is called.

Jim Lewis
  • 43,505
  • 7
  • 82
  • 96
  • How do I create a new Myclass object if it's not a pointer to an object? – p4storr Nov 28 '18 at 23:30
  • Declare object within Insert if you want a new one each time. That might create other problems if there's a legitimate reason that object is currently global – Phil M Nov 28 '18 at 23:51
0

How about this?

Myclass *object;

void insert()
{
    object = new Myclass();
    object->doSomething();
    //Myclass * ptr = object;
    HashTable.insert(object);
}

Edit:

PS: Make sure you iterate through the hash table and delete all the pointers, else you will have a huge bunch of dangling pointers

Explanation

You need to ensure your code does not modify old object inside the hash table each time insert is called. So, we make object a pointer and instantiate each time the method insert is called.

Sahil Dhoked
  • 372
  • 2
  • 12
  • Already did, but it takes like 3 minutes to generate an output and before it ends printing the output it gives me a segmentation fault. – p4storr Nov 28 '18 at 23:30
  • Recommend moving `Myclass *object;` into `insert`, no need for global here, add soem explanation of what you're doing and why, and drop in a reminder to `delete` all of the ... Never mind. You just added that last bit. – user4581301 Nov 28 '18 at 23:30
  • 1
    @p4storr you have another bug in that case. Sahil's answer is the most direct correct solution to your current problem. – user4581301 Nov 28 '18 at 23:31
  • @user4581301 Definitely would recommend moving Myclass *object into insert. But we don't really know why it was global in the first place. One example could be the code needs to track the last MyClass. Will add some explanantion – Sahil Dhoked Nov 28 '18 at 23:33
  • @p4storr what exactly takes 3 miuntes. Also, please share details of the segmentation fault. Is it due to memory leak? – Sahil Dhoked Nov 28 '18 at 23:34
  • This is currently a nice, clean question. Adding the printing error to it makes the question too broad and less useful to those who follow. The printing error is a different question and should be treated as a different question. – user4581301 Nov 28 '18 at 23:40
  • Maybe you can periodically remove elements from the Hashtable (and delete them) – Sahil Dhoked Nov 28 '18 at 23:48