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. 8 3039
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.
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::vec tor<int>()); // release all memory
]
HTH,
M4
"Maximus" <ma*******@vide otron.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
"Maximus" <ma*******@vide otron.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
"Maximus" <ma*******@vide otron.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
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::vec tor<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
"Maximus" <ma*******@vide otron.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
On Thu, 15 Jan 2004 15:21:33 -0800, Tom wrote: v.swap(std::vec tor<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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 elements, attributes, ".", and "..",
plus also the "" predicate format is supported -
however, only one predicate per path step is supported, and expr must
be a relative path.
2. Poor performance
|
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)
|
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 using cascade delete. When I use a continuous form
and a record is deleted, Access provides a warning that there are
related records, do you want to continue (an aside - anyone know how to
trap that warning on the form on error event?). However,...
|
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 the instance variable
initialize to NULL / 0 ?
- if there is not enough memory what happend with new ? does it return NULL
or throw an exception?
- if new throw a native C++ exception what happen in Managed C++ ?!
- if there is an exception in...
|
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 ID="hylForms" Runat="server"
NavigateUrl="#">Forms</asp:HyperLink>
Codebehind:
protected HyperLink hylForms;
| |
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
oledbdataadapter from the toolbox into my form.
Everything worked fine on my local computer. I was able to search, update,
delete and insert into my klanten.mdb database.
|
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 (i.e. the
root directory). This is the code:
<script runat="server">
Sub MoveFile(ByVal obj As Object, ByVal ea As EventArgs)
File.Move(fudFileSource.FileName, txtFileDest.Text)
'File.Move("F:\4.jpg", "C:\4.jpg")
End Sub
|
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.
Here is a description of the three functions:
Insert: Accepts as input the array and the element you want to insert into the array. The function should insert the element at the end of the array and should call the function Sort to sort the...
|
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 meets these
requirements, but after reading the chapter on exceptions in
'Exceptional C++':Sutter, I am not sure if its is really Exception safe
or Exception Neutral. I suppose putting the theory in that chapter into
practice isn't trivial.
|
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 & update to the database based on the hidden ID.
DELETE ALL will delete everything the databse.
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |