Google Groups Home
Help | Sign in
Why people use "new" & "delete" too much?!!
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  Messages 1 - 25 of 32 - Collapse all   Newer >
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
Medvedev  
View profile
 More options Jul 5, 3:39 pm
Newsgroups: comp.lang.c++
From: Medvedev <3D.v.Wo...@gmail.com>
Date: Sat, 5 Jul 2008 12:39:00 -0700 (PDT)
Subject: 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;
  .
  .
  ...
  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

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
red floyd  
View profile
 More options Jul 5, 3:59 pm
Newsgroups: comp.lang.c++
From: red floyd <no.spam.h...@example.com>
Date: Sat, 05 Jul 2008 12:59:56 -0700
Local: Sat, Jul 5 2008 3:59 pm
Subject: Re: Why people use "new" & "delete" too much?!!

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.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Medvedev  
View profile
 More options Jul 5, 4:05 pm
Newsgroups: comp.lang.c++
From: Medvedev <3D.v.Wo...@gmail.com>
Date: Sat, 5 Jul 2008 13:05:49 -0700 (PDT)
Local: Sat, Jul 5 2008 4:05 pm
Subject: Re: Why people use "new" & "delete" too much?!!
On Jul 5, 11:59 am, red floyd <no.spam.h...@example.com> wrote:

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

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Somebody  
View profile
 More options Jul 5, 4:13 pm
Newsgroups: comp.lang.c++
From: "Somebody" <someb...@cox.net>
Date: Sat, 5 Jul 2008 13:13:09 -0700
Local: Sat, Jul 5 2008 4:13 pm
Subject: Re: Why people use "new" & "delete" too much?!!
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(new classB(param1));
classa->InsertItem(new 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.v.Wo...@gmail.com> wrote in message

news:9575b4d8-e4d9-4c0b-a4f8-aefe7864ae33@k30g2000hse.googlegroups.com...


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ian Collins  
View profile
 More options Jul 5, 11:18 pm
Newsgroups: comp.lang.c++
From: Ian Collins <ian-n...@hotmail.com>
Date: Sun, 06 Jul 2008 15:18:39 +1200
Local: Sat, Jul 5 2008 11:18 pm
Subject: Re: Why people use "new" & "delete" too much?!!

Medvedev wrote:
> On Jul 5, 11:59 am, red floyd <no.spam.h...@example.com> wrote:
>> 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.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
acehr...@gmail.com  
View profile
 More options Jul 6, 12:29 am
Newsgroups: comp.lang.c++
From: acehr...@gmail.com
Date: Sat, 5 Jul 2008 21:29:12 -0700 (PDT)
Local: Sun, Jul 6 2008 12:29 am
Subject: Re: Why people use "new" & "delete" too much?!!
On Jul 5, 1:13 pm, "Somebody" <someb...@cox.net> wrote:

> 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(new classB(param1));
> classa->InsertItem(new 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(&classB(param1));
classa->InsertItem(&classC(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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
phlip  
View profile
 More options Jul 6, 12:55 am
Newsgroups: comp.lang.c++
From: phlip <phlip2...@gmail.com>
Date: Sat, 05 Jul 2008 21:55:13 -0700
Local: Sun, Jul 6 2008 12:55 am
Subject: Re: Why people use "new" & "delete" too much?!!

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

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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Salt_Peter  
View profile
 More options Jul 6, 1:08 am
Newsgroups: comp.lang.c++
From: Salt_Peter <pj_h...@yahoo.com>
Date: Sat, 5 Jul 2008 22:08:09 -0700 (PDT)
Local: Sun, Jul 6 2008 1:08 am
Subject: Re: Why people use "new" & "delete" too much?!!
On Jul 5, 4:05 pm, Medvedev <3D.v.Wo...@gmail.com> wrote:

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_ptr to name
a few.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Somebody  
View profile
 More options Jul 6, 2:19 am
Newsgroups: comp.lang.c++
From: "Somebody" <someb...@cox.net>
Date: Sat, 5 Jul 2008 23:19:15 -0700
Local: Sun, Jul 6 2008 2:19 am
Subject: Re: Why people use "new" & "delete" too much?!!

<acehr...@gmail.com> wrote in message

news:c0357384-87f2-46c7-a874-3a498f73e4a7@m44g2000hsc.googlegroups.com...

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

They had a bunch of stuff like:

ctrl->InsertItem(new ButtonTypeA(param1, param2));
ctrl->InsertItem(new ButtonTypeB(param3, param4));
ctrl->InsertItem(new ButtonTypeC(param4, 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(param1, param2);
ctrl->InsertItem(param3, param4);
ctrl->InsertItem(param4, param5);

or

ctrl->InsertButtonTypeA(param1, param2);
ctrl->InsertButtonTypeB(param3, param4);
ctrl->InsertButtonTypeC(param4, param5);

or

ctrl->InsertItem(TYPE_A, param1, param2);
ctrl->InsertItem(TYPE_B, param3, param4);
ctrl->InsertItem(TYPE_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.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
James Kanze  
View profile
 More options Jul 6, 5:59 am
Newsgroups: comp.lang.c++
From: James Kanze <james.ka...@gmail.com>
Date: Sun, 6 Jul 2008 02:59:23 -0700 (PDT)
Local: Sun, Jul 6 2008 5:59 am
Subject: Re: Why people use "new" & "delete" too much?!!
On Jul 5, 9:59 pm, red floyd <no.spam.h...@example.com> wrote:

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:james.ka...@gmail.com
Conseils en informatique orientée objet/
                   Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
James Kanze  
View profile
 More options Jul 6, 6:04 am
Newsgroups: comp.lang.c++
From: James Kanze <james.ka...@gmail.com>
Date: Sun, 6 Jul 2008 03:04:20 -0700 (PDT)
Local: Sun, Jul 6 2008 6:04 am
Subject: Re: Why people use "new" & "delete" too much?!!
On Jul 5, 10:05 pm, Medvedev <3D.v.Wo...@gmail.com> wrote:

> On Jul 5, 11:59 am, red floyd <no.spam.h...@example.com> wrote:

    [...]

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

Objects don't have scope, they have lifetime.  Scope concerns
the visibility of a declaration (and is linked with the
structure of the program).  Lifetime concerns when the object
comes into and goes out of being.  C++ defines several different
types of lifetime, some linked to scope (e.g. automatic), and
others not (e.g. dynamic).  If you create an object with new, it
has dynamic lifetime, and exists until you delete it.  Which
could be somewhere else entirely.

--
James Kanze (GABI Software)             email:james.ka...@gmail.com
Conseils en informatique orientée objet/
                   Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34