473,386 Members | 1,702 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,386 software developers and data experts.

std::allocator

Hi,

can anybody tell me where I can get the boiler plate code for
std::allocator. I need to have my version of new and delete called and
want to get reference code. My compilers headers are all over the place
with #ifdefs and what not, I'd like a clean copy.
thanks much

GrahamO

Nov 22 '05 #1
7 3043
Gr*****@nospam.com wrote:
Hi,

can anybody tell me where I can get the boiler plate code for
std::allocator.
Implementation defined.
I need to have my version of new and delete called and
want to get reference code. My compilers headers are all over the place
with #ifdefs and what not, I'd like a clean copy.


Just define new and delete operators for your class, they will get
called instead of the global ones.

# include <iostream>

class Test
{
public:
void *operator new(std::size_t s)
{
std::cout << "hello";
return 0;
}

void operator delete(void *p)
{
std::cout << "goodbye";
}
};

int main()
{
delete new Test; // hellogoodbye
}
Jonathan

Nov 22 '05 #2
Is it safe to do that ? I mean, I'm using custom iterators for legacy
containers already and I need my version of new and delete to be called
by STL algorithms (which already use my custom Iterators).

i.e. I am going to be using stl algorithms with custom containers and
it's essential that our version of new and delete be called. Fair
enough, if yes, just wanted to check.

thanks

G

Nov 22 '05 #3
Gr*****@nospam.com wrote:
Is it safe to do that ? I mean, I'm using custom iterators for legacy
containers already and I need my version of new and delete to be called
by STL algorithms (which already use my custom Iterators).

i.e. I am going to be using stl algorithms with custom containers and
it's essential that our version of new and delete be called. Fair
enough, if yes, just wanted to check.

thanks

G


Actually, my recollection/experience is that STL containers will use
placement new rather than "regular" operator new. (Since the allocation
of memory is handled separately by the Allocator.) Thus you need to define:

void* operator new(size_t size, void* loc);

In fact, if you only define:

void* operator new (size_t size);

you will likely get compilation errors because that definition will hide
the global placement new needed by the STL containers.

Mark

// EXAMPLE

#include <new>
#include <list>

struct C
{
// If you define this...
void* operator new(std::size_t size) {return ::operator new(size);}
// then you need to define this...
void* operator new(std::size_t size, void* loc) {
return ::operator new(size,loc);}
};

int main()
{
std::list<C> cList;
// otherwise this won't compile:
cList.push_back(C());
}
Nov 22 '05 #4
The standard containers won't ever call new to allocate memory -
they'll use
allocator supplied with template instantiation and placement new to
invoke
the constructor.

What you probably want to do is to create custom allocator, but not for
use
by new. (I guess your original question was just that).

The API requirements for the allocator are as follows:
(but please, don't quote me on this and better find a refernce
to it in the standard).

template <typename T>
class allocator
{
public:
//Typedefs
typedef T value_type;
typedef value_type * pointer;
typedef value_type const* const_pointer;
typedef value_type& reference;
typedef value_type const& const_reference;
typedef size_t size_type;
typedef ptrdiff_t difference_type;
public:
//The rebind
template <typename U>
struct rebind
{
typedef allocator<U> other;
};
public:
//Constructors/Destructors
allocator() throw() {};
allocator(allocator const&) throw() {};
~allocator() throw() {};

template <typename U>
allocator(allocator<U> const&) throw() {};
//address
pointer address(reference r) const { return &r; };
const_pointer address(const_reference r) const { return &r; };

//memory allocation / deallocation
pointer allocate(size_type cnt, void const * = 0); //Custom
allocation here

void deallocate(pointer p, size_type cnt) throw(); //Custom
deallocation here
//size
size_type max_size() const throw();

//object construction / destrucion
void construct(pointer p, T const& t) const { new (p) T(t); };
void destroy(pointer p) const throw() { p->~T(); };
bool operator == (allocator const&) { return true; };
bool operator != (allocator const& a) { return false; };

};


Gr*****@nospam.com wrote:
Is it safe to do that ? I mean, I'm using custom iterators for legacy
containers already and I need my version of new and delete to be called
by STL algorithms (which already use my custom Iterators).

i.e. I am going to be using stl algorithms with custom containers and
it's essential that our version of new and delete be called. Fair
enough, if yes, just wanted to check.

thanks

G


Nov 22 '05 #5
Mark P wrote:
Gr*****@nospam.com wrote:
Is it safe to do that ? I mean, I'm using custom iterators for legacy
containers already and I need my version of new and delete to be called
by STL algorithms (which already use my custom Iterators).

i.e. I am going to be using stl algorithms with custom containers and
it's essential that our version of new and delete be called. Fair
enough, if yes, just wanted to check.

thanks

G


Actually, my recollection/experience is that STL containers will use
placement new rather than "regular" operator new.


Ok, I am getting in a field I don't know. I never worked much with
custom allocators or overloading new and delete, so I'll make some
guesses and leave others to respond.

The standard explictly states that std::allocator::construct() has the
effect of
"new((void*)p) T(t)", which clearly does not explicitly use global new
operator. That means it should use placement new defined for a class.
However, the compiler I use (VS 2005) has "::new (p) T(v);" instead,
but I think it is illegal.

Concerning allocate() and deallocate(), I found nothing in the standard
stating whether the operators used are global or not, but Josuttis says
"The default allocator uses the global operators new and delete to
allocate and deallocate memory". So that probably rules out overloaded
operators.

So finally, I think you *will* need to provide your own allocator. You
may have a look at http://www.open-std.org/jtc1/sc22/open/n2356/. It is
the 1997 draft C++ standard. Particularily, 20.1.5 describes
requirements and 20.4.1 describes the class interface. Josuttis' "The
C++ Standard Library" also does a good job explaining allocators and I
think I remember reading something in TCPPPL.

Hope this helps,
Jonathan

Nov 22 '05 #6
Andy Venikov wrote:
The standard containers won't ever call new to allocate memory -
they'll use
allocator supplied with template instantiation and placement new to
invoke
the constructor.


The default allocator uses operator new to get memory for storing
objects. It then uses placement new, as appropriate, to construct
objects in the memory that it got.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Nov 22 '05 #7
Sorry, let me refrase:

The standard containers won't call new DIRECTLY to allocate memory.
If new is getting called, then only through the allocation policy
supplied to the container. And I think that's what the OP wanted.

Thanks,
Andy.

Nov 22 '05 #8

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

Similar topics

3
by: Orjan Westin | last post by:
Hi, I have an interesting (read frustrating) problem. I'm writing a generic container class, which holds data as well as links to other instances of itself, like this: template<class T>...
0
by: ScOe | last post by:
I've made a container class over std::allocator. Also implement create, clear, push_back but i need some assistance on removing item from container. do i need to create empty container and than...
20
by: Anonymous | last post by:
Is there a non-brute force method of doing this? transform() looked likely but had no predefined function object. std::vector<double> src; std::vector<int> dest; ...
5
by: Vinu | last post by:
Hi I am facing a problem in compilation the error is like this In constructor xServices::CServices<TImp>::StHoldClientList::StHoldClientList(std::set<TImp*, std::less<TImp*>,...
1
by: hokus | last post by:
How to code (generally) std::allocator class to use HeapAlloc function? Thanks
13
by: kamaraj80 | last post by:
Hi I am using the std:: map as following. typedef struct _SeatRowCols { long nSeatRow; unsigned char ucSeatLetter; }SeatRowCols; typedef struct _NetData
1
by: sharmadeep1980 | last post by:
Hi All, I am facing a very unique problem while compling my project in "Release" build. The project is building in DEBUG mode but giving linking error on Release build. Here is the error:...
8
by: Anamika | last post by:
Can I have maps with following properties? Key= A character strings... Value=A structure... Members of structure will be two character strings.... If yes how to do that?Can anyone tell me the...
2
by: pssraju | last post by:
Hi, At present application was built on solaris 9 using sun studio 9 (Sun C++ 5.6) & rouguewave sorce pro 5. We are planning to port the same application onto SuSE Linux 9.5.0 using GCC 3.3.3 & RW...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...

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.