Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #13903

Re: Question about destructors

From 88888 Dihedral <dihedral88888@googlemail.com>
Newsgroups comp.lang.c++
Subject Re: Question about destructors
Date 2012-02-11 08:17 -0800
Organization http://groups.google.com
Message-ID <4050810.418.1328977066073.JavaMail.geo-discussion-forums@pbcth8> (permalink)
References <ba1d69e7-bd02-4c9a-9ab0-1f674fb3bc56@do4g2000vbb.googlegroups.com> <xn0hu2h1h91v2i4002@news.astraweb.com> <f7b07275-5374-4d4e-a679-44d9eb4cd719@eb6g2000vbb.googlegroups.com> <bc06893f-6438-4493-88cc-ef30998f9362@hs8g2000vbb.googlegroups.com>

Show all headers | View raw


在 2012年2月10日星期五UTC+8下午8时35分19秒,SG写道:
> On Feb 7, 6:13 pm, Gus Gassmann wrote:
> >
> > Some of the code is not written by me, so I am afraid Fred`s solution
> > does not work. Let me then summarize, just to be absolutely clear:
> >
> > > 4. Object **obj = new Object[n];
> > >    for (int i=0; i<n; i++) obj[i] = new Object();
> >
> >      for (int i=0; i<n; i++) delete obj[i];
> >      delete[] obj;
> >
> > > 5. Object *obj = new Object();
> > >    obj->intArray = new int[10];
> >
> >      delete[] obj->intArray;
> >      delete obj;
> >
> > > 6. Object **obj = new Object[n];
> > >    for (int i=0; i<n; i++) {
> > >         obj[i] = new Object();
> > >         obj[i]->intArray = new int[10];
> > >    }
> >
> >      for (int i=0; i<n; i++) {
> >          delete[] obj[i]->intArray;
> >          delete obj[i];
> >      }
> >      delete[] obj;
> >
> > Will that do it?
> 
> Not really. This is pretty fragile C++ code and doomed to break
> eventually. For example, it's not exception-safe. Try to exploit the
> strengths of C++. One of the biggest corner stones of modern C++
> design is letting objects "own"/take care of other resources. C++
> allows you to delegate the responsibility of cleaning up things so you
> don't have to do it yourself.
> 
>   Fragile:
>     int* p = new int[10];
>     ...
>     delete[] p;
> 
>   Good:
>     vector<int> v (10);
>     ...
> 
> To put it differently: If you want memory to be released if a pointer
> goes out of scope, the raw pointer type would be a misuse because it
> doesn't have the semantics you actually want.
> 
> Instead of new[] and delete[], prefer std::vector. Instead of "naked"
> new, prefer the use of std::unique_ptr (or std::shared_ptr) so that
> every heap-allocated object is "owned" by at least one other object.
> This makes "delete" in user code rather obsolete. And even "new"
> should be avoided in preference of std::make_shared and
> std::make_unique (make_unique is not yet standard but you can write it
> yourself
> 
>   template<class T, class...Args>
>   std::unique_ptr<T> make_unique(Args&&...args)
>   {
>     return {new T(std::forward<Args>(args)...)};
>   }
> 
> ). In case you are worried about unnecessary copying with respect to
> vectors and functions returning function-local vectors by value,
> you'll be pleased to know that C++2011 will make sure this is about as
> efficient as returning a raw pointer.
> 
> Cheers!
> SG

In C++  objects in  classes with several vectors or other containers
are allowed. Even copying an object is not so trivial.

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Question about destructors Gus Gassmann <horand.gassmann@googlemail.com> - 2012-02-07 06:53 -0800
  Re: Question about destructors "Fred Zwarts \(KVI\)" <F.Zwarts@KVI.nl> - 2012-02-07 16:11 +0100
  Re: Question about destructors "Asger Joergensen" <Junk@Asger-P.dk> - 2012-02-07 15:38 +0000
    Re: Question about destructors Gus Gassmann <horand.gassmann@googlemail.com> - 2012-02-07 09:13 -0800
      Re: Question about destructors "Asger Joergensen" <Junk@Asger-P.dk> - 2012-02-07 17:34 +0000
        Re: Question about destructors Victor Bazarov <v.bazarov@comcast.invalid> - 2012-02-07 12:42 -0500
          Re: Question about destructors 88888 Dihedral <dihedral88888@googlemail.com> - 2012-02-07 23:39 -0800
            Re: Question about destructors Ian Collins <ian-news@hotmail.com> - 2012-02-08 22:32 +1300
              Re: Question about destructors Gareth Owen <gwowen@gmail.com> - 2012-02-08 17:51 +0000
                Re: Question about destructors Gert-Jan de Vos <gert-jan.de.vos@onsneteindhoven.nl> - 2012-02-08 10:47 -0800
      Re: Question about destructors SG <s.gesemann@gmail.com> - 2012-02-10 04:35 -0800
        Re: Question about destructors 88888 Dihedral <dihedral88888@googlemail.com> - 2012-02-11 08:17 -0800
  Re: Question about destructors Joshua Maurice <joshuamaurice@gmail.com> - 2012-02-07 15:12 -0800
  Re: Question about destructors Juha Nieminen <nospam@thanks.invalid> - 2012-02-08 06:38 +0000
    Re: Question about destructors Peter Remmers <p.remmers@expires-2011-04-30.arcornews.de> - 2012-02-09 00:30 +0100
      Re: Question about destructors Peter Remmers <p.remmers@expires-2011-04-30.arcornews.de> - 2012-02-09 00:56 +0100
      Re: Question about destructors Joshua Maurice <joshuamaurice@gmail.com> - 2012-02-09 14:34 -0800
        Re: Question about destructors Jorgen Grahn <grahn+nntp@snipabacken.se> - 2012-02-13 17:48 +0000

csiph-web