By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,960 Members | 1,298 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,960 IT Pros & Developers. It's quick & easy.

new & delete

P: n/a
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.
Jul 22 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Maximus wrote:
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.

Jul 22 '05 #2

P: n/a
On Thu, 15 Jan 2004 20:35:38 +1300, Jacques Labuschagne wrote:
Maximus wrote:
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
Jul 22 '05 #3

P: n/a
"Maximus" <ma*******@videotron.ca> wrote in message
news:B5**********************@weber.videotron.net. ..
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

Jul 22 '05 #4

P: n/a

"Maximus" <ma*******@videotron.ca> wrote in message
news:B5**********************@weber.videotron.net. ..
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

Jul 22 '05 #5

P: n/a

"Maximus" <ma*******@videotron.ca> skrev i en meddelelse
news:B5**********************@weber.videotron.net. ..
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.
TIA,Max.


Use std::vector (or std::deque) and use the resize member function.
/Peter
Jul 22 '05 #6

P: n/a
Tom
Martijn Lievaart <m@remove.this.part.rtij.nl> wrote:
[ 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
Jul 22 '05 #7

P: n/a

"Maximus" <ma*******@videotron.ca> wrote in message
news:B5**********************@weber.videotron.net. ..
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
Jul 22 '05 #8

P: n/a
On Thu, 15 Jan 2004 15:21:33 -0800, Tom wrote:
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);


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

M4

Jul 22 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.