🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Just one more library function

#4064
From: Igor Boukanov
Date: Tue, 16 Apr 1996 20:15
52 lines
2277 bytes
    Why do not require that every C++ implementation should provide function 
that will return for the pointer obtained from new[] the number of 
allocated elements, i.e. something like:

template<class T> size_t array_allocation_size(T* p);

So for 'int* p = new int[3];' 'array_allocation_size(p)' will return 3.

Such function will be very easy to implement because in the case of
delete[] compiler use something like this. So why do not make it available 
for programmer?

In this case it will be possible to emulate placement delete for arrays, i.e.
one can write:

template<class T> void array_placement_delete(T* p) {
   size_t i = array_allocation_size(p);
   try { //attempt to support throw in destructors
      while (i > 0) {
         --i;
         p[i].T::~T();
      }
   }
   catch(...) {
      try {
         while(i > 0) {
            --i;
            p[i].T::~T();
         }
      catch(...) {
         terminate();
      }
      throw();
   }
}

and array size doesn't need to be passed to array_placement_delete like in 
the ordinary delete[] case...

So what do you think?

--
Regards, Igor Boukanov. 
igor.boukanov@fi.uib.no  
http://www.fi.uib.no/~boukanov/
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your 
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu 
]
Technical Details
Message-ID: <Pine.HPP.3.91.960416133333.19297A-100000@hadron.fi.uib.no>
Path: arc1-ca.pugleaf.net!fido.asd.sgi.com!austern

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!fido.asd.sgi.com!austern
From: Igor Boukanov <Igor.Boukanov@fi.uib.no>
Newsgroups: comp.std.c++
Subject: Just one more library function
Date: 16 Apr 1996 13:15:25 PDT
Organization: Fysisk institutt, Universitetet i Bergen
Approved: austern@isolde.mti.sgi.com
Message-ID: <Pine.HPP.3.91.960416133333.19297A-100000@hadron.fi.uib.no>
NNTP-Posting-Host: isolde.mti.sgi.com
Keywords: C++, array allocation
X-Original-Date: Tue, 16 Apr 1996 13:35:30 +0200 (METDST)
X-Auth: PGPMoose V1.1 PGP comp.std.c++
 iQBVAwUBMXP/3ky4NqrwXLNJAQF1PQH+Oz8hPB4xL86CtVrQAvSggZZY/+pWBVF3
 JgXh4w7izuyrKvDRJM76LJn6hKIc/h+kOm+B0UgciwKaPruzIGXUkg==
 =smCf
Originator: austern@isolde.mti.sgi.com