Topic: ANSI/ISO new[] and delete[]
Author: sprocket@cbnewsi.cb.att.com (dean.s.jones)
Date: Wed, 29 Dec 1993 18:17:40 GMT Raw View
Could someone bring me up to date on the new[] and delete[] operators
proposed in ANSI/ISO C++. I have a copy of the ANSI/ISO Resolutions (Appendix
A for the ARM) from world.std.com. It states:
r.12.5
Two operators are introduced specifically to handle allocation and
deallocation of arrays:
void* operator new[](size_t);
void operator delete[](void*);
void operator delete[](void*, size_t);
The use of the two forms of delete[] corresponds to their delete
equivalents.
Since there is only one size_t parameter, I assume it's value is defined as
N_ELEMENTS * sizeof(T). I would have to derive the number of elements with
N_ELEMENTS = size / sizeof(T) if I needed that value.
The ARM, Section 12.5 states that new & delete are inherited if they
have been defined for a class.
To keep derived classes from using our per-class memory pools we use
the size parameter in the alternate form of delete described in 12.5 and the
size parameter from new to help us check these allocations:
void* T::operator new(size_t size)
{
if(size != sizeof(T))
{
// a warning ???
return ::new char[size];
}
else
{
return Some_Pool_Of_T.allocate();
}
}
void T::operator delete(void *memp, size_t size)
{
if(size != sizeof(T))
{
// a warning ???
::delete [] memp;
}
else
{
Some_Pool_Of_T.free(memp);
}
}
So... These if these array allocators and deallocators are inherited and
not re-defined, how can I protect myself from derived class allocations.
I could check to see if size is evenly divisible by sizeof(T), but thats
no where near good enough. Imagine:
class X : public T
where sizeof(X) == 2 * sizeof(T) or sizeof(X) is any multiple of sizeof(T)
or sizeof(T) == 1!!!. Would this be an array of N class T's or N/2 class
X's ??? The same thing for the (alternate) delete operator.
Any help???
Dean Jones
dean@hos1cad.att.com
Author: sprocket@cbnewsi.cb.att.com (dean.s.jones)
Date: 5 Jan 94 17:59:30 GMT Raw View
This is my second posting, it seems the first one was lost somewhere in the
queue...
Could someone bring me up to date on the new[] and delete[] operators
proposed in ANSI/ISO C++. I have a copy of the ANSI/ISO Resolutions (Appendix
A for the ARM) from world.std.com. It states:
r.12.5
Two operators are introduced specifically to handle allocation and
deallocation of arrays:
void* operator new[](size_t);
void operator delete[](void*);
void operator delete[](void*, size_t);
The use of the two forms of delete[] corresponds to their delete
equivalents.
Since there is only one size_t parameter, I assume it's value is defined as
N_ELEMENTS * sizeof(T). I would have to derive the number of elements with
N_ELEMENTS = size / sizeof(T) if I needed that value.
The ARM, Section 12.5 states that new & delete are inherited if they
have been defined for a class.
To keep derived classes from using our per-class memory pools we use
the size parameter in the alternate form of delete described in 12.5 and the
size parameter from new to help us check these allocations:
void* T::operator new(size_t size)
{
if(size != sizeof(T))
{
return ::new char[size];
}
else
{
return Some_Pool_Of_T.allocate();
}
}
void T::operator delete(void *memp, size_t size)
{
if(size != sizeof(T))
{
::delete [] memp;
}
else
{
Some_Pool_Of_T.free(memp);
}
}
So... These if these array allocators and deallocators are inherited and
not re-defined, how can I protect myself from derived class allocations.
I could check to see if size is evenly divisible by sizeof(T), but thats
no where near good enough. Imagine:
class X : public T
where sizeof(X) == 2 * sizeof(T) or sizeof(X) is any multiple of sizeof(T)
or sizeof(T) == 1!!!. Would this be an array of N class T's or N/2 class
X's ??? The same thing for the (alternate) delete operator.
Any help???
Dean Jones
dean@hos1cad.att.com
Could it also be N * sizeof(T) + 4 ( to hold the number of bytes for delete ) ??