🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

2 total messages Started by Igor Boukanov Tue, 16 Apr 1996 20:15
Just one more library function
#4064
Author: 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 
]
Re: Just one more library function
#4070
Author: Jason Merrill
Date: Wed, 17 Apr 1996 08:49
25 lines
1771 bytes
>>>>> Igor Boukanov <Igor.Boukanov@fi.uib.no> writes:

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

Not necessarily.  For instance, g++ only stores the number of elements if 

1) The element type has a destructor, or
2) The element type has an operator delete that takes the size_t argument.

Jason
---
[ comp.std.c++ is moderated.  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                             ]
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads