473,378 Members | 1,457 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,378 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 2993
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: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.