views:

2317

answers:

3

Suppose I have a Linked List I created myself. It has its own destructor, which frees the memory. This Linked List does not overload new or delete.

Now, I'm trying to create an array of said linked lists (open hashing, if I understand correctly). Then I allocate the necessary memory inside the constructor of this open hashing class. The new operator being called inside the constructor is enough to correctly allocate the memory for the array, right? I'm not sure because I haven't overloaded new for the Linked List class.

Also, assuming my array of Linked Lists is called elements, could I just write "delete[] elements" in the destructor? Would that call the destructor for each element in the array and correctly free the memory?

Finally, if both my assumptions are correct (ie, I don't have to overload new and delete to use them with my custom class), what is the point of overloading such operators?

+4  A: 

Yeah you are right. A plain

elements = new LinkedList[N];

is enough to allocate them. You can then access them

elements[i]->push(....);

and delete them in your destructor using the way you showed:

delete[] elements;

The compiler will remember how many elements were allocated, and call the destructor for each list correctly. The point of overloading the new and delete operator is to provide custom memory allocation strategy. For example, you could preallocate memory, and then take from that pool, instead of allocating everytime again memory from the OS.

But note, you have to write a copy constructor and copy assignment operator too. Since if someone copies your hash map, the linked list has to be copied too, and not just the pointer. Or you can make the copy constructor and copy assignment operator private and don't define them, disallowing copies of your hash map:

....
private:
    MyHashMap(MyHashMap const& rhs);
    MyHashMap & operator=(MyHashMap const& rhs);
....
Johannes Schaub - litb
one small nitpick:Most calls to new do not involve the os. Generally the OS only gets involved when the heap needs to grab more virtual memory pages.
Scott Wisniewski
+1  A: 

All of your assumptions are correct.

There are lots of uses for overloading new and delete, but it is not done often. One common reason is for tracking memory allocations in order to spot memory leaks. A lot of compile time leak trackers do this, but have sort of become obsolete with better external apps like valgrind. You can also do things like use pooled memory.

SoapBox
+1  A: 

The new operator does two things: allocating memory and calling the constructor.

The delete operator calls the destructor and then frees the memory.

Arrays created with new [] must be destroyed with delete[].

You generally don't need to overload new or delete except for performance reasons. You might have a predictable pattern of allocation/deallocation which makes a particular allocation strategy very suitable (fast or low memory use).

You may wish to have a look at this page.

Artelius