473,706 Members | 2,301 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why people use "new" & "delete" too much?!!

i see serveral source codes , and i found they almost only use "new"
and "delete" keywords to make they object.
Why should i do that , and as i know the object is going to be destroy
by itself at the end of the app

for example:
class test
{
public:
int x;
}

int main(int argc, char **argv)
{
test *n= new test;
Jul 5 '08 #1
30 3826
Medvedev wrote:
i see serveral source codes , and i found they almost only use "new"
and "delete" keywords to make they object.
Why should i do that , and as i know the object is going to be destroy
by itself at the end of the app

for example:
class test
{
public:
int x;
}

int main(int argc, char **argv)
{
test *n= new test;
.
.
...
delete n;

return 0;
}
i know that the object created this way is in the heap which have much
memory than stack but why they always define objects that way , why
not just say "test n" and the object will be destroyed by itself at
the end of the program! , instead of using "new" and maybe u will
forget to "delete" at the end
Several reasons.

1. They're coming from Java and they don't know any better
2. They're storing polymorphic objects inside containers
3. They need the lifetime of the object to exceed the scope in which
it was declared.

Jul 5 '08 #2
On Jul 5, 11:59 am, red floyd <no.spam.h...@e xample.comwrote :
Medvedev wrote:
i see serveral source codes , and i found they almost only use "new"
and "delete" keywords to make they object.
Why should i do that , and as i know the object is going to be destroy
by itself at the end of the app
for example:
class test
{
public:
int x;
}
int main(int argc, char **argv)
{
test *n= new test;
.
.
...
delete n;
return 0;
}
i know that the object created this way is in the heap which have much
memory than stack but why they always define objects that way , why
not just say "test n" and the object will be destroyed by itself at
the end of the program! , instead of using "new" and maybe u will
forget to "delete" at the end

Several reasons.

1. They're coming from Java and they don't know any better
2. They're storing polymorphic objects inside containers
3. They need the lifetime of the object to exceed the scope in which
it was declared.
how u can use object after it's scope ends!!
Jul 5 '08 #3
class CFoo
{
};

CFoo* GetFoo()
{
return new CFoo;
}

Still, I agree with you, people over use new and delete. I recently saw a
class library written in C++ that tried to be all C#'ish and required you to
write code like:

classa->InsertItem(n ew classB(param1)) ;
classa->InsertItem(n ew classC(param2)) ;

*STUPID*... if your code is too stupid to decide what kind of object should
be created (and keep in mind, I could see doing this for user defined
objects, but these were all internal objects), then why would you expect a
user of your class to?

Plus, that hurts performance.

"Medvedev" <3D********@gma il.comwrote in message
news:95******** *************** ***********@k30 g2000hse.google groups.com...
On Jul 5, 11:59 am, red floyd <no.spam.h...@e xample.comwrote :
>Medvedev wrote:
i see serveral source codes , and i found they almost only use "new"
and "delete" keywords to make they object.
Why should i do that , and as i know the object is going to be destroy
by itself at the end of the app
for example:
class test
{
public:
int x;
}
int main(int argc, char **argv)
{
test *n= new test;
.
.
...
delete n;
return 0;
}
i know that the object created this way is in the heap which have much
memory than stack but why they always define objects that way , why
not just say "test n" and the object will be destroyed by itself at
the end of the program! , instead of using "new" and maybe u will
forget to "delete" at the end

Several reasons.

1. They're coming from Java and they don't know any better
2. They're storing polymorphic objects inside containers
3. They need the lifetime of the object to exceed the scope in which
it was declared.

how u can use object after it's scope ends!!

Jul 5 '08 #4
Medvedev wrote:
On Jul 5, 11:59 am, red floyd <no.spam.h...@e xample.comwrote :
>3. They need the lifetime of the object to exceed the scope in which
it was declared.

how u can use object after it's scope ends!!
I don't know about "u", but the rest of us don't.

A pointer to a dynamically allocated object can be returned from the
function that created it, or declared in an outer scope and assigned in
an inner one.

--
Ian Collins.
Jul 6 '08 #5
On Jul 5, 1:13 pm, "Somebody" <someb...@cox.n etwrote:
Still, I agree with you, people over use new and delete. I recently saw a
class library written in C++ that tried to be all C#'ish and required you to
write code like:

classa->InsertItem(n ew classB(param1)) ;
classa->InsertItem(n ew classC(param2)) ;

*STUPID*...
It's not obvious what you say that. Is it because the dynamic objects
are not handed to smart pointers right away? Or do you propose
something like the following?

classa->InsertItem(&cl assB(param1));
classa->InsertItem(&cl assC(param2));

You realize that the temporaries would be terminated too soon to be
useful?
Plus, that hurts performance.
Compared to what? If it's needed, then there is no alternative.

Ali
Jul 6 '08 #6
Medvedev wrote:
i see serveral source codes , and i found they almost only use "new"
and "delete" keywords to make they object.
If you write an OO program, you will find yourself needing a base class pointer
that points to a derived class. (That is the very goal of "OO" - to override
some critical method into that derived class.)

Otherwise, you could construct an object on the stack, use it, and let it
destroy when its method returns.

Because you need dynamically sized and typed objects, you must sometimes new them.
Why should i do that , and as i know the object is going to be destroy
by itself at the end of the app
You should code as if you don't know that. Always clean up after yourself. One
good way is with "smart pointers".
for example:
class test
{
public:
int x;
}

int main(int argc, char **argv)
{
test *n= new test;
.
.
...
delete n;

return 0;
}
i know that the object created this way is in the heap which have much
memory
Not necessarily. On modern architectures with virtual memory, both the heap and
stack can grow arbitrarily.
than stack but why they always define objects that way , why
not just say "test n" and the object will be destroyed by itself at
the end of the program! , instead of using "new" and maybe u will
forget to "delete" at the end
Because that's one of the many things C++ will let you do that are sloppy. Don't
do any of them, because any one of them could come back to bite you on the butt.

For example, you could refactor the n = new test and move it inside a working
loop. Then the loop would silently leak (virtual!) memory. You would not notice
until your program ran for hours, and got very slow.

--
Phlip
Jul 6 '08 #7
On Jul 5, 4:05 pm, Medvedev <3D.v.Wo...@gma il.comwrote:
On Jul 5, 11:59 am, red floyd <no.spam.h...@e xample.comwrote :
Medvedev wrote:
i see serveral source codes , and i found they almost only use "new"
and "delete" keywords to make they object.
Why should i do that , and as i know the object is going to be destroy
by itself at the end of the app
for example:
class test
{
public:
int x;
}
int main(int argc, char **argv)
{
test *n= new test;
.
.
...
delete n;
return 0;
}
i know that the object created this way is in the heap which have much
memory than stack but why they always define objects that way , why
not just say "test n" and the object will be destroyed by itself at
the end of the program! , instead of using "new" and maybe u will
forget to "delete" at the end
Several reasons.
1. They're coming from Java and they don't know any better
2. They're storing polymorphic objects inside containers
3. They need the lifetime of the object to exceed the scope in which
it was declared.

how u can use object after it's scope ends!!
Thats not what he stated. The object is _declared_ in a finite scope.
If you allocate the object on the heap, it's lifetime no longer relies
on the declaring scope.

Basicly, new and new[] transfers the responsability to you, the
programmer, to delete and delete[].

Is using new and new[] a good habit? no, its not.
You'll find C++ programmers to be retiscent in using it and would
rather rely on a smart pointer if heap allocation is indeed required.
Java programmers don't really have a choice but in C++ an automatic
variable should and usually is the default.

Generally speaking, if you see new and new[], you aren't reading a
programmer who has his roots in modern C++. Allocating on the heap
what should be automatic is frowned upon here.

And the reason for that is because smart pointers have much to offer
as long as you know their limitations.
Good examples of those are std::auto_ptr and boost::shared_p tr to name
a few.

Jul 6 '08 #8

<ac******@gmail .comwrote in message
news:c0******** *************** ***********@m44 g2000hsc.google groups.com...
On Jul 5, 1:13 pm, "Somebody" <someb...@cox.n etwrote:
>Still, I agree with you, people over use new and delete. I recently saw a
class library written in C++ that tried to be all C#'ish and required you
to
write code like:

classa->InsertItem(n ew classB(param1)) ;
classa->InsertItem(n ew classC(param2)) ;

*STUPID*...

It's not obvious what you say that. Is it because the dynamic objects
are not handed to smart pointers right away? Or do you propose
something like the following?

classa->InsertItem(&cl assB(param1));
classa->InsertItem(&cl assC(param2));

You realize that the temporaries would be terminated too soon to be
useful?
>Plus, that hurts performance.

Compared to what? If it's needed, then there is no alternative.

Ali
Well, let me de-anonimize the code a bit :)...

They had a bunch of stuff like:

ctrl->InsertItem(n ew ButtonTypeA(par am1, param2));
ctrl->InsertItem(n ew ButtonTypeB(par am3, param4));
ctrl->InsertItem(n ew ButtonTypeC(par am4, param5));

So they had a UI control, and they were inserting items into it. ButtonTypeX
was a class *the class library* defined... not something that a *user* of
the class library would (or could) define.

What I was saying... was I failed to see why I should do the dirty work for
the library and not only determine what class to use, but also to allocate
it for them. They should determine whether to create the internal
ButtonTypeA, ButtonTypeB, ButtonTypeC, etc. in some other way (like a param
for example)... so I'd rather see something like:

ctrl->InsertItem(par am1, param2);
ctrl->InsertItem(par am3, param4);
ctrl->InsertItem(par am4, param5);

or

ctrl->InsertButtonTy peA(param1, param2);
ctrl->InsertButtonTy peB(param3, param4);
ctrl->InsertButtonTy peC(param4, param5);

or

ctrl->InsertItem(TYP E_A, param1, param2);
ctrl->InsertItem(TYP E_B, param3, param4);
ctrl->InsertItem(TYP E_C, param4, param5);

Like other posters said, that style of code is C#'ish or Java'ish... it is
not typical C++ style. Unless you are doing some type of class factory type
thing.

Allocating memory is slow... which is why this particular UI library was
commonly regarded as having poor performance.
Jul 6 '08 #9
On Jul 5, 9:59 pm, red floyd <no.spam.h...@e xample.comwrote :
Medvedev wrote:
i see serveral source codes , and i found they almost only
use "new" and "delete" keywords to make they object. Why
should i do that , and as i know the object is going to be
destroy by itself at the end of the app
for example:
class test
{
public:
int x;
}
int main(int argc, char **argv)
{
test *n= new test;
.
.
...
delete n;
return 0;
}
i know that the object created this way is in the heap which
have much memory than stack but why they always define
objects that way , why not just say "test n" and the object
will be destroyed by itself at the end of the program! ,
instead of using "new" and maybe u will forget to "delete"
at the end
Several reasons.
1. They're coming from Java and they don't know any better
2. They're storing polymorphic objects inside containers
Not just storing them inside containers. I've a couple of
places where I've code something like:

std::auto_ptr< Base obj(
someCondition
? static_cast< Base* >( new D1 )
: static_cast< Base* >( new D2 ) ) ;

It's not that common, however.
3. They need the lifetime of the object to exceed the scope in which
it was declared.
Often, the last two reasons go together: although there's no
formal link between them, in practice, polymorphic objects tend
to have arbitrary lifetimes.

Note that you normally would prefer copying an object to
extending its lifetime, if the object supports copy.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jul 6 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

30
3566
by: seesaw | last post by:
Is it right thing to always avoid using "new" to create objects? What if after starting the application, then decide which and how many objects to create? (Seems like under such situation is there no other choice but using "new")
24
2865
by: Rv5 | last post by:
Rookie c++ question, but Ive spent the last 5 years doing Java, where everytime I created an object I used new. In c++ I can create my objects without and its confusing me just a little. I have a class called polynomial. Its a nothing little class right now, with just int variables, a basic container class. Im using it as I go through some tutorials, but in this particular tutorial its telling me to do polynomial *first = new...
13
3102
by: gary | last post by:
Hi, We all know the below codes are dangerous: { int *p = new int; delete p; delete p; } And we also know the compilers do not delete p if p==NULL. So why compilers do not "p = NULL" automatically after programs do "delete p"?
5
8087
by: mkaushik | last post by:
Hi everyone, Im just starting out with C++, and am curious to know how "delete <pointer>", knows about the number of memory locations to free. I read somewhere that delete frees up space assigned to <pointerby "new". Does "new" create a list of pointer names and the size of the memory array they point to? I also read that some compilers may store the number of consec mem locations a pointer points to, just before the first data...
350
11759
by: Lloyd Bonafide | last post by:
I followed a link to James Kanze's web site in another thread and was surprised to read this comment by a link to a GC: "I can't imagine writing C++ without it" How many of you c.l.c++'ers use one, and in what percentage of your projects is one used? I have never used one in personal or professional C++ programming. Am I a holdover to days gone by?
0
8696
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,...
0
9285
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9050
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,...
0
8993
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7906
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6614
isladogs
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...
0
4448
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...
0
4709
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2094
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.