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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Medvedev

    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;
  • red floyd

    #2
    Re: Why people use "new&qu ot; & "delete&qu ot; too much?!!

    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.

    Comment

    • Medvedev

      #3
      Re: Why people use "new&qu ot; & "delete&qu ot; too much?!!

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

      Comment

      • Somebody

        #4
        Re: Why people use &quot;new&qu ot; &amp; &quot;delete&qu ot; 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(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.v.World@gma il.comwrote in message
        news:9575b4d8-e4d9-4c0b-a4f8-aefe7864ae33@k3 0g2000hse.googl egroups.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!!

        Comment

        • Ian Collins

          #5
          Re: Why people use &quot;new&qu ot; &amp; &quot;delete&qu ot; too much?!!

          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.

          Comment

          • acehreli@gmail.com

            #6
            Re: Why people use &quot;new&qu ot; &amp; &quot;delete&qu ot; too much?!!

            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

            Comment

            • phlip

              #7
              Re: Why people use &quot;new&qu ot; &amp; &quot;delete&qu ot; 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".
              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

              Comment

              • Salt_Peter

                #8
                Re: Why people use &quot;new&qu ot; &amp; &quot;delete&qu ot; too much?!!

                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.

                Comment

                • Somebody

                  #9
                  Re: Why people use &quot;new&qu ot; &amp; &quot;delete&qu ot; too much?!!


                  <acehreli@gmail .comwrote in message
                  news:c0357384-87f2-46c7-a874-3a498f73e4a7@m4 4g2000hsc.googl egroups.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.


                  Comment

                  • James Kanze

                    #10
                    Re: Why people use &quot;new&qu ot; &amp; &quot;delete&qu ot; too much?!!

                    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:james.kan ze@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

                    Comment

                    • James Kanze

                      #11
                      Re: Why people use &quot;new&qu ot; &amp; &quot;delete&qu ot; too much?!!

                      On Jul 5, 10:05 pm, Medvedev <3D.v.Wo...@gma il.comwrote:
                      On Jul 5, 11:59 am, red floyd <no.spam.h...@e xample.comwrote :
                      [...]
                      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.kan ze@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

                      Comment

                      • James Kanze

                        #12
                        Re: Why people use &quot;new&qu ot; &amp; &quot;delete&qu ot; too much?!!

                        On Jul 6, 8:19 am, "Somebody" <someb...@cox.n etwrote:
                        <acehr...@gmail .comwrote in message
                        news:c0357384-87f2-46c7-a874-3a498f73e4a7@m4 4g2000hsc.googl egroups.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.
                        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.
                        But he could almost certainly define a class which derived from
                        it, and use that instead.
                        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)...
                        It's very likely that all three calls above invoke the same
                        InsertItem. Even if they don't, it's almost certain that the
                        user code could derive from the different ButtonType's, and pass
                        an instance of the derived class, rather than the base class.
                        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.
                        It's very typical C++ where polymorphism and arbitrary lifetime
                        is involved, which is almost certainly the case here.

                        C++ is a multi-paradigm language. There are cases when the
                        above paradigm is appropriate, C++ supports it, and it should be
                        used in such cases.
                        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.
                        Practically speaking, things like buttons in a GUI must have
                        arbitrary lifetime; they can't be automatic variables. So if
                        you don't do the new, the library must. And you immediately
                        loose the flexibility of deriving from the object. And of
                        course, things like Button's typically also have identity; if
                        you attach an event handler to one instance, but a copy receives
                        the event, then your code isn't going to work.

                        --
                        James Kanze (GABI Software) email:james.kan ze@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

                        Comment

                        • Pascal J. Bourguignon

                          #13
                          Re: Why people use &quot;new&qu ot; &amp; &quot;delete&qu ot; too much?!!

                          "Somebody" <somebody@cox.n etwrites:
                          Allocating memory is slow... which is why this particular UI library was
                          commonly regarded as having poor performance.
                          Only in C++, because there's no garbage collector. In languages with
                          automatic memory management, allocating is free.

                          --
                          __Pascal Bourguignon__

                          Comment

                          • Sam

                            #14
                            Re: Why people use =?UTF-8?B?Im5ldyI=?= &amp; =?UTF-8?B?ImRlbGV0ZSI =?=too much?!!

                            Pascal J. Bourguignon writes:
                            "Somebody" <somebody@cox.n etwrites:
                            >Allocating memory is slow... which is why this particular UI library was
                            >commonly regarded as having poor performance.
                            Only in C++, because there's no garbage collector. In languages with
                            automatic memory management, allocating is free.
                            Right. Which is why, at $day job$, a recently accomplished task was to dump
                            a market data feed application that was written in Java, and replace it with
                            C++ code -- because its garbage collection is just sooooooooo… fast, and
                            heap usage is soooooo… little.

                            Not.


                            -----BEGIN PGP SIGNATURE-----
                            Version: GnuPG v1.4.9 (GNU/Linux)

                            iEYEABECAAYFAkh x+DwACgkQx9p3GY HlUOKh2ACfc4FPN 1ShP1U0rXoZK7Bh zwPC
                            H2sAnRzsiRqy/Pfug68wwjlINGww PRFB
                            =nPJC
                            -----END PGP SIGNATURE-----

                            Comment

                            • Yannick Tremblay

                              #15
                              Re: Why people use &quot;new&qu ot; &amp; &quot;delete&qu ot; too much?!!

                              In article <7cd4lqf3az.fsf @pbourguignon.a nevia.com>,
                              Pascal J. Bourguignon <pjb@informatim ago.comwrote:
                              >
                              >Only in C++, because there's no garbage collector. In languages with
                              >automatic memory management, allocating is free.
                              Sorry, it isn't free.

                              The performance/cost characteristic of allocating and releasing memory
                              in an automatic memory managed language is different. Under some
                              circumstances, it can be orders of magnitude faster than C++
                              explicit memory management but it certainly isn't free and things can
                              happen if you get out of a particualr set of "some circumstances"


                              Yannick


                              Comment

                              Working...