0

I want to understand something about dynamic memory: new and delete for a class template as below.

I have class template defined by some library(in house developed) which defines the class template as below.

template <class DAT, class IDX, int ENTRYMX=0x20, int SUBMX=34, int bsz=8> 
class HashTable : public public HashTableIn
{
HashTable () 
: HashTableIn(ENTRYMX),
  ...


...
...
}

In my code I use an object of that template class as:

mpRt = new HashTable<data_st,index_st>;

and I delete this mpRt explicitly as( I know smart pointers... but currently they arent used in this legacy code so lets leave them out)

delete mpRt;

My question: I am trying to understand whether Do i have to do

delete [] mpRt

because the class template has a default argument of ENTRYMX = 0x20
Scooter
  • 6,802
  • 8
  • 41
  • 64
goldenmean
  • 18,376
  • 54
  • 154
  • 211

4 Answers4

2

I am trying to understand whether Do i have to do delete [] mpRt

No, you allocate mpRt by operator new then you call delete mpRt; You have to call new/delete and new [] /delete[] in pair.

If you use ENTRYMX inside HashTable to dynamically allocate memory with new[], you need to call delete[] inside HashTable to deallocate it. Also don't forget to follow Rule of Three.

Community
  • 1
  • 1
billz
  • 44,644
  • 9
  • 83
  • 100
  • In C++ 11 rule of Three now is the Rule of five(move semantics). Very strange question for user with such rating, maybe some additional info is omitted and goldenmean should edit his question to make it more clear/ although +1 for evident answer ) – spin_eight Aug 21 '13 at 10:15
  • @spin_eight It's still rule of three. Supporting move semantics is an optimization, only done when the profiler says you must. (On the other hand, it's more or less a non-rule today: application level classes should generally be designed so that the defaults do the right thing.) – James Kanze Aug 21 '13 at 10:25
  • @ James Kanze supporting move semantics add`s move constructor and move assignement operator therefore the Rule of 3 evolves to the Rule of 5 ) – spin_eight Aug 21 '13 at 10:33
2

From the standard :

5.3.4 New [expr.new]

8/ If the allocated type is a non-array type, the allocation function’s name is operator new and the deallocation function’s name is operator delete. If the allocated type is an array type, the allocation function’s name is operator new[] and the deallocation function’s name is operator delete[].

new comes always with delete.

new [] comes always with delete [].

In your case, mpRt is allocated with the operator new, you must deallocate it with delete.

Community
  • 1
  • 1
Pierre Fourgeaud
  • 14,290
  • 1
  • 38
  • 62
  • It's more complex than that. First, even if he did allocate an array, `mpRt` would have exactly thee same type. Secondly, it's important to realize that `new[]` has nothing to do with the tokens used in the `new` expression; it's quite possible to have an array `new` without any `[]`, or a plain `new` with `[]` in the new expression. You have to analyze the type itself, to see if it is actually an array. – James Kanze Aug 21 '13 at 10:32
1

Why would you ? mpRt isn't an array allocated with new[].

It may contain an array (as hints the argument name ENTRYMX), but mpRt isn't of an array type in itself. The management of the memory related to this array is internal to the class, and not the responsibility of the code that creates object of this class.

Rule of thumb : match news with deletes and new type[]s with delete []s.

JBL
  • 12,588
  • 4
  • 53
  • 84
1

No, always use new/delete and new[]/delete[] in pair. So don't use delete[] if you haven't explicitly used new[]

swang
  • 5,157
  • 5
  • 33
  • 55