J
Jacques Labuschagne
Maximus said:Hi,
I was wondering if it is bad to overuse the new & delete operator. In my
application, e.g. I created my own list class and I will have to resize my
variable maybe like 100 times during runtime (if not more). By resize I mean
somthing like:
int* a = new int[30];
delete a;
delete[] a; // You're deleting an array, not a single object.
Excessive use of new/delete can slow a program down quite remarkably. If
speed isn't an issue, use them all you like.
If you've got a vector-ish object you want to grow or shrink as needed,
the rules of thumb are
- Be lazy about shrinking, i.e. don't shrink every time an element
is popped off
- Don't grow by 1 element each time something is added; double your
capacity each time the limit is hit.
Jacques.