new & delete

  • Thread starter Jacques Labuschagne
  • Start date
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.
 
M

Maximus

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;
a = new int[40];
delete a;
a = new int[100];

on and on...

It works well but is it the "good" way?

TIA,Max.
 
C

Chris Theis

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;
a = new int[40];
delete a;
a = new int[100];

on and on...

It works well but is it the "good" way?

It's not the "good" but actually a "wrong" way because you need to issue a
delete [] a;
statement to delete an array and not a single object! Anyway, excessive
reallocation is prone to slow down your system. Therefore so called "chunk
allocation" will come in handy. The trick is to allocate a chunk of memory
of a fixed size and keep book how much is used of it. In case that the
required amount is exceeded you allocate an additional chunk of memory and
so. But why aren't you using the list class supplied by the standard library
which is optimized?

Chris
 
M

Martijn Lievaart

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.

Agreed, and 100 allocations/deallocations will hardly be noticeable on
todays hardware. But a million or more, that is when you have to start
thinking. But after you've shown it to be a problem, first make it
correct, then make it fast.
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

In fact, don't shrink at all, unless you know you need it.
- Don't grow by 1 element each time something is added; double your
capacity each time the limit is hit.

And there is a very easy way to implement the two items above, use
std::vector<> instead of manually creating/resizing the array.

[ A warning about standard vector. Although you can decrease its size
(the numer of elements stored), you cannot shrink its capacity (the amount
of memory used). The best you can do is release all memory with this trick:

std::vector<int> v;

v.swap(std::vector<int>()); // release all memory

]

HTH,
M4
 
D

David White

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;
a = new int[40];
delete a;

delete[] a;
a = new int[100];

on and on...

It works well but is it the "good" way?

There's a performance cost, but probably worse is the maintainability cost.
It's better to use standard library collections (std::vector, std::list,
etc.) if you can. They do news and deletes as well, but at least you don't
have to worry about them. Standard collections save you code-writing time,
they are written to be reasonably efficient, and they keep low-level,
mundane stuff out of your application code, which makes your code smaller
and easier to understand and maintain.

DW
 
P

Peter Koch Larsen

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;
a = new int[40];
delete a;
a = new int[100];

on and on...

It works well but is it the "good" way?

Should not work because of the errors above.

Use std::vector (or std::deque) and use the resize member function.


/Peter
 
T

Tom

Martijn Lievaart said:
[ A warning about standard vector. Although you can decrease its size
(the numer of elements stored), you cannot shrink its capacity (the amount
of memory used). The best you can do is release all memory with this trick:

std::vector<int> v;

v.swap(std::vector<int>()); // release all memory

That won't work, because the argument to member function swap must be
a reference, not const reference, so it can't be a temporary. I think
you mean:

std::vector<int>().swap(v);

Best regards,

Tom
 
J

Jonathan Turkanis

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

Isn't it always bad to overuse something? ;-)

Jonathan
 
M

Martijn Lievaart

That won't work, because the argument to member function swap must be
a reference, not const reference, so it can't be a temporary. I think
you mean:

std::vector<int>().swap(v);

I use this trick to infrequently, so I got it wrong. Thanks for the
correction.

M4
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,272
Latest member
MaricruzDu

Latest Threads

Top