🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #3562

Re: operators new[]/delete[]

#3562
From: Tony Cook
Date: Fri, 01 Mar 1996 14:55
76 lines
3247 bytes
Pablo Halpern (phalpern@truffle.ultranet.com) wrote:
: Although I don't agree with most of what Constantine Antonovich says, I
: do wonder about one particular set of situations. Many allocation and
: re-allocation systems use the default new() operator to allocate memory
: in the form of char arrays. This "raw" memory is then recast to an array
: of specific type, which is initialized one element at a time:

:   template <class T>
:   T* dup_array(const T* p, size_t s)
:   {
:     T *p2 = reinterpret_cast<T*> new char[s * sizeof(T)];  // note 1
:     while (s-- > 0)
:       new (p2 + s) T(p[s]);  // Initialize using copy constructor
:   }

:   void f(T* p, size_t s)
:   {
:     T* newp = dup_array(p, s);
:     // do something with newp
:     delete [] newp;     // note 2
:   }

: I believe that something similar to the line marked "note 1" is common
: practice for this sort of operation. However, I believe that the line
: marked "note 2" is undefined behavior. 

Yes it is.

: Is there a way in the standard
: can be modified so that the above code becomes well-defined and works as
: intended? 

This isn't likely - most delete[] implementations where a
non-trivial destructor is involved will use extra information before
the beginning of the array - which isn't present in your example
(and that information is implementation dependent, so you can't set
it portably in your own code).

: How does the STL deal with this in its "unitialized copy"
: operation?

It destroys the original objects using their destructors (and the
containers call operator delete to release the memory.)

For example:
   template <class T>
   void f(const T* p, size_t s)
   {
     p += s;
     while (s-- > 0)
       (--p)->~T();
     operator delete(p);
   }


: There is another way to allocate raw memory, but here the operations are
: even less defined (I believe):

:    void *p2 = operator new[] (s * sizeof(T));
:    delete p2;  // What does this do? p2 was not the result of a normal
:                // new expression.
:    delete reinterpret_cast<T*> p2;  // What does this do?

You should use:
	operator delete[](p2);

-- 
        Tony Cook - tony@online.tmx.com.au
                    100237.3425@compuserve.com
---
[ To submit articles: try just posting with your news-reader.
                      If that fails, use mailto:std-c++@ncar.ucar.edu
  FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html
  Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu.
]
Technical Details
Message-ID: <199602292206.JAA27760@online.tmx.com.au>
Path: arc1-ca.pugleaf.net!in1.uu.net!bounce-back
References: <313166CF.11C2@orbotech.co.il> <4gu414$62u@mulga.cs.mu.OZ.AU> <4h24c2$pq@caesar.ultra.net>

Show Full Headers
Complete Article Headers
From: tony@online.tmx.com.au (Tony Cook)
Message-ID: <199602292206.JAA27760@online.tmx.com.au>
X-Original-Date: Fri, 1 Mar 1996 09:06:10 +1100
Path: arc1-ca.pugleaf.net!in1.uu.net!bounce-back
Date: 01 Mar 96 14:55:27 GMT
Approved: fjh@cs.mu.oz.au
Newsgroups: comp.std.c++
Subject: Re: operators new[]/delete[]
Organization: Home
References: <313166CF.11C2@orbotech.co.il> <4gu414$62u@mulga.cs.mu.OZ.AU> <4h24c2$pq@caesar.ultra.net>
X-Newsreader: TIN [version 1.2 PL2]
X-Auth: PGPMoose V1.1 PGP comp.std.c++
 iQBFAgUBMTcP8OEDnX0m9pzZAQFzQAF/YFZdEaZ+yZiHKdYK5/71JRkiMyUxGy7g
 3vHJc3sKpPyfyp40C409CNsZVnHGcScF
 =2WE/