From 35601856713780990
X-Google-Language: ENGLISH,ASCII
X-Google-Thread: f78e5,8366734ec6aad121
X-Google-Attributes: gidf78e5,public
X-Google-ArrivalTime: 1994-06-14 03:24:41 PST
Path: nntp.gmd.de!xlink.net!zib-berlin.de!informatik.tu-muenchen.de!schuenem
From: schuenem@Informatik.TU-Muenchen.DE (Ulf Schuenemann)
Newsgroups: comp.std.c++
Subject: Re: new foo[0]
Date: 14 Jun 1994 10:22:50 GMT
Organization: Technische Universitaet Muenchen, Germany
Lines: 91
Distribution: world
Message-ID: <2tk0dq$6ej@hpsystem1.informatik.tu-muenchen.de>
References: <CquD6C.LEK@lehman.com> <1994Jun6.152021.20287@cs.wm.edu> <JASON.94Jun6201445@deneb.cygnus.com>
NNTP-Posting-Host: hpbroy6.informatik.tu-muenchen.de
Originator: schuenem@hpbroy6.informatik.tu-muenchen.de


In article <JASON.94Jun6201445@deneb.cygnus.com>, jason@cygnus.com (Jason Merrill) writes:
|> >>>>> Adrian Filipi-Martin <adrian@mo.cs.wm.edu> writes:
|> 
|> >         Has anyone ever considered providing a mechanism for either
|> > accessing the fields of the internal data or validating it from within
|> > C++? The inaccessibility of this data was what made finding the bug so
|> > difficult. Even an implementation dependent mechanism would be
|> > appreciated.
|> 
|> Here's the way to do it in g++ currently.  The 'union foo' bit is to force
|> the pointer to be aligned like a double.  Note that the cookie will not be
|> used unless the class has either a destructor or an operator delete [] that
|> takes the optional size_t argument, so array_size cannot be applied to all
|> arrays allocated by new.
|> 
|> Jason
|> 
|> typedef unsigned long size_t;
|> extern "C" int printf (const char *, ...);
|> 
|> size_t array_size (void *p)
|> {
|>   union foo {
|>     struct __new_cookie {
|>       size_t nelts;
|>     } c;
|>     double d;
|>   };
|> 
|>   foo *fp = (foo *)p;
|>   --fp;
|> 
|>   return fp->c.nelts;
|> }
|> 
|> struct A {
|>   ~A() { }
|> };
|> 
|> main()
|> {
|>   A *ap = new A[3];
|>   printf ("%ld\n", array_size (ap));
|> }
|>

To implement the delete[]-operator new[] must store information about
size of the array somewhere (Is there a way to do without it?).
I would appreciate if there were a STANDARD for obtaining this
information at runtime (with a new keyword - oh, how ugly. I'll
call it XXX here):
With pa being a kind of reference(pointer) to an array:

	XXX(pa)

returns the number of elements in the array refered to by pa.
To work in C++ pa must not be a pointer to an element (single object
or somewhere in an array), but a refenence to (the beginning of)
an array. It's type would be declared by:

	T  (&pa)[];

This pa may only refere to the beginning of an array, not to somewhere
inside it. So "int a[]10], (&pa)[]=a" is allowed but ".. (&pa)[]=&a[2]"
is not. It should then be possible:


void fct(int *p1, int p2[], int (&pa)[])
{
	printf("sizes= %d %d %d elems=%d\n",sizeof(p1),sizeof(p2),sizeof(pa),XXX(pa));

}
void main()
{	int	a[10];
	fct(a,a,a);
}

results in "sizes= 4 4 40 elems=10"

I would prefere this over some array_size-hack (see above). What is
your opinion?

Ulf Schuenemann

--------------------------------------------------------------------
Ulf Sch�nemann
Institut f�r Informatik, Technische Universit�t M�nchen.
email: schuenem@informatik.tu-muenchen.de
WWW:   http://hphalle2/~schuenem  (currently not available from outside)



