473,659 Members | 2,722 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_t ype cnt, typename
std::allocator< void>::const_po inter hint = 0)
{
pointer p = reinterpret_cas t<pointer(new char[sizeof(T)*cnt]);
std::cout<<"MEM ORY ADDRESS: "<<p<<std::endl ; //FIRST PRINT-OUT!
return p;
}

Then I have a regular container in my main:
int main()
{
typedef std::multiset<F oo,less<Foo>,Fo oAllocator<Foo MYSET;

MYSET mine;

MYSET::iterator iter = mine.insert(Foo ());
std::cout<<"ITE R = "<<&(*iter)<<st d::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 1898
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_t ype cnt, typename
std::allocator< void>::const_po inter hint = 0)
{
pointer p = reinterpret_cas t<pointer(new char[sizeof(T)*cnt]);
This is probably the cause of your problem. reinterpret_cas t is not a
safe cast and it does not guarantee proper object alignment.

F
std::cout<<"MEM ORY ADDRESS: "<<p<<std::endl ; //FIRST PRINT-OUT!
return p;
}

Then I have a regular container in my main:
int main()
{
typedef std::multiset<F oo,less<Foo>,Fo oAllocator<Foo MYSET;

MYSET mine;

MYSET::iterator iter = mine.insert(Foo ());
std::cout<<"ITE R = "<<&(*iter)<<st d::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_t ype cnt, typename
std::allocator< void>::const_po inter hint = 0)
{
pointer p = reinterpret_cas t<pointer(new char[sizeof(T)*cnt]);
std::cout<<"MEM ORY 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<F oo,less<Foo>,Fo oAllocator<Foo MYSET;

MYSET mine;

MYSET::iterator iter = mine.insert(Foo ());
std::cout<<"ITE R = "<<&(*iter)<<st d::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(c onst 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_t ype cnt, typename
std::allocator< void>::const_po inter hint = 0)
{
pointer p = reinterpret_cas t<pointer(new char[sizeof(T)*cnt]);
std::cout<<"MEM ORY ADDRESS: "<<p<<std::endl ; //FIRST PRINT-OUT!
return p;
}
Then I have a regular container in my main:
int main()
{
typedef std::multiset<F oo,less<Foo>,Fo oAllocator<Foo MYSET;
MYSET mine;
MYSET::iterator iter = mine.insert(Foo ());
std::cout<<"ITE R = "<<&(*iter)<<st d::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 objektorientier ter Datenverarbeitu ng
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
2418
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 new memory may be allocated. It also needs to be thread safe, and each thread has a context, so any allocation of nodes currently looks like this: new (context) Node(...);
15
4266
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
4124
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 people would back me up on this. The design basically uses a pool of memory, allocated as a character array. Pointers into the array are retured by the allocated function. Isn't this very dangerous, as a char has very lenient memory alignment...
4
2942
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 minimal overhead and be used for allocation of a large number of "Node" classes which represent a hierarchical structure. The custom allocator has to be at least intrusive as possible for the user. The allocator has to be somehow bound to the...
3
2141
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. Nevertheless the allocator produces "Segmentation fault (core dumped)". Is it possible to correct that allocator in order to avoid such behavior ------ foo.cpp ------
3
1706
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 than just saying to derive from allocator... Can anybody help...
0
1309
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 on top of that memory. I expected my "construct" method to be called to handle this, but no one calls it. No more than MAX_OBJS object of class Obj will ever be allocated. #include<iostream> #include <set>
2
3918
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 did not support nested templates classes, so could not use the rebind struct. Instead, a _Charalloc function was implemented. Now that we are moving to VS 2005, this is no longer working so I need to implement using the rebind struct. So I did...
2
1666
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); alloc.construct(ptr, Type(5)); The problem with this is that it requires 'Type' to have a copy constructor. It might not have one (ie. it might be disabled). Having the copy constructor disabled doesn't stop an object from being
0
8427
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8851
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8746
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8525
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7356
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6179
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4335
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2750
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.