473,386 Members | 1,830 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

new & delete

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
8 2994
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
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
"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

"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

"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
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

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: bdinmstig | last post by:
I refined my attempt a little further, and the following code does seem to work, however it has 2 major problems: 1. Very limited support for XPath features Basic paths are supported for...
4
by: Timothy Madden | last post by:
Hello Is my program legal and well formed: class Data; class SMax { class Data &Buffer; public: SMax(class Data &buffer)
3
by: Tim Marshall | last post by:
HI all, Access 2003, Jet back end. Rather than annoy my users in a particular app by having relationships with enforced relational integrity refuse to delete a record with related records, I'm...
11
by: Lloyd Dupont | last post by:
(not I use 2.0, so new return a "normal" pointer and gcnew return a managed one, my question below regarding new concern plain standart C++ allocator) - if I use the default new operator, are all...
6
by: FoundThisOnline | last post by:
I have been searching online for a whole day and couldn't find an answer for this. I have this hyperlink server control: ------------------------------------------------ <asp:HyperLink...
1
by: dirk van waes | last post by:
Hello everyone, Being complete newbie in asp.net I am trying to make an example which works with a very simple database. First I made my project in VS- vb.net, draging an oledbconnection and an...
3
by: Arpan | last post by:
A Form has a FileUpload, 2 Buttons & a TextBox web server controls. Using the FileUpload control, I want to give users the provision to move & delete files that DO NOT exist in C:\Inetpub\wwwroot...
6
by: flash | last post by:
write a program that manipulates arrays of integers. The main program should call three functions: Insert, Delete, and Search. The Insert function should call a function Sort that sorts the array. ...
3
by: Nindi73 | last post by:
Hi, I am in need of a deep copy smart pointer (Boost doesn't provide one) which doesnt require the contained types to have a virtual copy constructor. I wrote a smart pointer class that I think...
0
by: Slickuser | last post by:
From my PHP page: Grab all data from the database. Go through a loop to generate the HTML. Client side: From the Color drop menu list, if a user change the value. It will grab that value &...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.