🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: operators new[]/delete[]

#3550
From: Pablo Halpern
Date: Thu, 29 Feb 1996 06:15
53 lines
2717 bytes
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. Is there a way in the standard
can be modified so that the above code becomes well-defined and works as
intended? How does the STL deal with this in its "unitialized copy"
operation?

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?

How do we solve these problems in a standard-conforming way. Does there
need to be special language for char arrays or void * allocations. (Or
is there already? I haven't found it.)

Thanks,
      
Pablo Halpern                   phalpern@truffle.ultranet.com

I am self-employed. Therefore, my opinions *do* represent 
those of my employer.
---
[ 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: <4h24c2$pq@caesar.ultra.net>
Path: arc1-ca.pugleaf.net!in1.uu.net!bounce-back
References: <313166CF.11C2@orbotech.co.il> <4gu414$62u@mulga.cs.mu.OZ.AU>

Show Full Headers
Complete Article Headers
From: phalpern@truffle.ultranet.com (Pablo Halpern)
Message-ID: <4h24c2$pq@caesar.ultra.net>
X-Original-Date: Wed, 28 Feb 1996 17:45:33 GMT
Path: arc1-ca.pugleaf.net!in1.uu.net!bounce-back
Date: 29 Feb 96 06:15:51 GMT
Approved: fjh@cs.mu.oz.au
Newsgroups: comp.std.c++
Subject: Re: operators new[]/delete[]
Organization: UltraNet Communications, Inc.
References: <313166CF.11C2@orbotech.co.il> <4gu414$62u@mulga.cs.mu.OZ.AU>
X-Newsreader: Forte Agent .99b.113
X-Auth: PGPMoose V1.1 PGP comp.std.c++
 iQBFAgUBMTVExOEDnX0m9pzZAQFKjAF/VI9a6LQ4v/MLK8RoeJuDxkGrM/0iQZ1I
 MdQ4uhevCWQeA5oJ7y3YpclVPbQmyjoL
 =jGxQ