473,320 Members | 1,810 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Custom Allocator Question - Advanced.

joe
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]);
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

May 23 '07 #1
3 1885
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
May 23 '07 #2
joe wrote:
pointer allocate(size_type cnt, typename
std::allocator<void>::const_pointer hint = 0)
{
pointer p = reinterpret_cast<pointer(new char[sizeof(T)*cnt]);
std::cout<<"MEMORY ADDRESS: "<<p<<std::endl; //FIRST PRINT-OUT!
return p;
}
Just for fun change the log message to this:
std::cout <<"MEMORY ADDRESS: " << p << ", sizeof T is"
<< sizeof(T) << std::endl;
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.
&(*iter) is not the address of the iterator, it's the address of the
object "pointed" to by the iterator. As you can see, the object has a
different address to one you returned from the allocate call. This may
be because the instance of the allocator template that is being used is
probably not the one you think is being used.

Presumably you had to define a rebind struct, can you see what has happened?

If not, add some logging to your constructors, especially the templated one.
template<class UFooAllocator(const FooAllocator<U>&)

Charles.
May 23 '07 #3
On May 23, 9:46 pm, joe <joeyc...@mail.comwrote:
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]);
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.
Presumably (I don't see how one could implement it differently),
what is being allocated in the multiset is not a T, but some
sort of node structure containing a T, typically with a couple
of pointers before the T. And the actual type of the allocator
being used to do the allocation should not be T, but this node
type.

Having said this, I don't quite see how this could be causing
you to overwrite anything. The allocation expression should be
something like (typedef's expanded, etc.):

__Alloc::rebind<__Node>::other( myAlloc ).allocate( n )

If you're rebind declaration is correct, and the code for
allocate is really what you show, then you should have no
trouble in this respect; if you're not sure, add output of
typeid(T).name and sizeof(T) to your allocator, to see what is
going on.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

May 24 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Brian Genisio | last post by:
Hi all, I am developing some software, that creates a tree of information. For each node, currently I am overriding the new operator, because it is a requirement that after initialization, no...
15
by: Alex Vinokur | last post by:
I am looking for any custom allocator sample code for std::vector. Thanks. -- Alex Vinokur http://mathforum.org/library/view/10978.html http://sourceforge.net/users/alexvn
4
by: Romeo Colacitti | last post by:
I have a need to make a custom quasi-memory allocator, and I remembered a simple ons in K&R2. Looking at the code for it now, I think I notice a "fault" in the design, and I was wondering if...
4
by: Robert Frunzke | last post by:
Hello, I need to implement a custom allocator to speed up the allocation of a specific class in my project and instead of hardwiring it, I would "templatize"(?) it. The allocator should have...
3
by: Alex Vinokur | last post by:
Compiler GNU g++ version 3.4.4 (cygming special) Custom allocator for vector (see below) checks a return value of 'operator new'. If that value is NULL, the allocator "allocates" no memory....
3
by: Giovanni | last post by:
Im trying to create a custom allocator for some stl lists im am using in a program. I have tried finding resources for it on the internet but i am unable to find anything that does a better job...
0
by: joe | last post by:
I have created a custom allocator for a multiset, and I am having a problem it seems because while the allocator has memory available to be used which is returned, the objects are never constructed...
2
by: ranin02 | last post by:
Hi, We have a list derived from std::list that has a custom allocator derived from std::allocator. This was originally written using VC++ 6.0 which required a workaround for the fact that 6.0...
2
by: Juha Nieminen | last post by:
If we have a custom allocator (eg. given to us as a template parameter), the proper way of allocating an object using it is like: Allocator alloc; Allocator::pointer ptr = alloc.allocate(1);...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.