About constructors / destructors and new / remote operations in C ++ for custom objects

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

Now I'm trying to create an array of the specified linked lists (open hashing, if I understand correctly). Then I allocate the necessary memory inside the constructor of this public hash class. The new statement called inside the constructor is enough to allocate memory for the array correctly, right? I'm not sure, because I did not overload the new ones for the Linked List class.

Also, if my array of linked lists is called elements, can I just write "delete [] elements" in the destructor? Could this cause a destructor for each element in the array and free memory properly?

Finally, if both of my assumptions are correct (i.e. I don’t need to overload new ones and delete them to use them with my custom class), what is the point of overloading such operators?

+3
c ++ constructor new-operator destructor
Dec 25 '08 at 2:46
source share
3 answers

Yes you are right. Plain

elements = new LinkedList[N]; 

enough to highlight them. Then you can 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 correctly call the destructor for each list. The overload point of the new and the deletion is to provide a custom memory allocation strategy. For example, you can preallocate memory and then take it from this pool instead of allocating memory from the OS every time.

But note, you also need to write a copy constructor and copy assignment operator. Because if someone copies your hash map, the linked list should also be copied, not just a pointer. Or you can make the copy statement and copy assignment statement private and not define them by denying copies of your hash map:

 .... private: MyHashMap(MyHashMap const& rhs); MyHashMap & operator=(MyHashMap const& rhs); .... 
+6
Dec 25 '08 at 2:51
source share

The new operator performs two functions: allocating memory and calling the constructor.

The delete statement calls the destructor, and then frees memory.

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

Usually you do not need to overload a new one or delete it , except for performance reasons. You may have a predictable allocation / release structure that makes a specific allocation strategy very suitable (using fast or low memory).

You can see this page .

+2
Dec 25 '08 at 3:00
source share

All your assumptions are correct.

There are many uses for reloading new ones and deleting them, but this is not done often. One common reason is monitoring memory allocation to detect memory leaks. Many compilation time-leak marks do this, but are kind of obsolete with better external applications like valgrind. You can also do things like using shared memory.

+1
Dec 25 '08 at 2:54
source share



All Articles