🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: Just one more library function

#4099
From: Steve Clamage
Date: Thu, 18 Apr 1996 15:40
45 lines
2506 bytes
In article fsf@yorick.cygnus.com, jason@cygnus.com (Jason Merrill) writes:
>>>>>> 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's answer mentions the overhead it would impose on all users whether
they want the feature or not.

I'd also like to point out that if you need to query an array about its
size, you probably should not be using a C-style array, but an array
class instead.

Finally, the proposal mentions only arrays on the heap. Is it the case that
you care about the size only of arrays allocated with operator new[],
and not static arrays? What about:
	extern T myarray[]; // how big is it?

If you use an array class, you can get the functionality you want, and
don't necessarily have to pay for functionality you don't want. If you
use the standard vector class, you don't even have to write any code.
---
Steve Clamage, stephen.clamage@eng.sun.com




[ 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                             ]
Technical Details
Message-ID: <4l5n3v$h7s@engnews1.Eng.Sun.COM>
Path: arc1-ca.pugleaf.net!engnews1.Eng.Sun.COM!taumet!clamage
References: <u93f63214o.fsf@yorick.cygnus.com>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!engnews1.Eng.Sun.COM!taumet!clamage
From: clamage@Eng.sun.com (Steve Clamage)
Newsgroups: comp.std.c++
Subject: Re: Just one more library function
Date: 18 Apr 1996 15:40:42 GMT
Organization: Sun Microsystems Inc.
Approved: clamage@eng.sun.com (comp.std.c++)
Message-ID: <4l5n3v$h7s@engnews1.Eng.Sun.COM>
References: <u93f63214o.fsf@yorick.cygnus.com>
Reply-To: clamage@Eng.sun.com
NNTP-Posting-Host: taumet.eng.sun.com
X-Nntp-Posting-Host: taumet.eng.sun.com
Content-Length: 1521
X-Lines: 38
Originator: clamage@taumet