473,790 Members | 2,850 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

allocator requirements

If you specify an allocator in an STL container is it a requirement that
the allocator allocates object of the right type, or can you assume that
the container will rebind the allocator to the correct type?

For example I tried the following code which uses a 'wrong' allocator and
was slightly surrised to find it compiles on the three compilers I tried
it on

#include <vector>
#include <memory>

int main()
{
std::vector<int , std::allocator< double> > vec;
vec.push_back(1 );
}

john
Jul 22 '05 #1
13 1951
John Harrison wrote:
If you specify an allocator in an STL container is it a requirement
that the allocator allocates object of the right type, or can you
assume that the container will rebind the allocator to the correct type?
It's quite possible that the container uses 'construct' member
function, which accepts the memory of any type (for placement
new) and double* is good enough for int values to be constructed
in. You probably get some excessive memory use that way, but
not any serious problem. Try it in reverse, have a vector of
double and use allocator<char> just for kicks...

As to 'rebind', I can't find any reference to it in the chapter
on containers. Assume you can, rely upon... I wouldn't.

For example I tried the following code which uses a 'wrong' allocator
and was slightly surrised to find it compiles on the three compilers I
tried it on

#include <vector>
#include <memory>

int main()
{
std::vector<int , std::allocator< double> > vec;
vec.push_back(1 );
}


V
Jul 22 '05 #2
On Fri, 09 Jul 2004 20:20:34 GMT, Victor Bazarov <v.********@com Acast.net>
wrote:
John Harrison wrote:
If you specify an allocator in an STL container is it a requirement
that the allocator allocates object of the right type, or can you
assume that the container will rebind the allocator to the correct
type?


It's quite possible that the container uses 'construct' member
function, which accepts the memory of any type (for placement
new) and double* is good enough for int values to be constructed
in. You probably get some excessive memory use that way, but
not any serious problem. Try it in reverse, have a vector of
double and use allocator<char> just for kicks...

As to 'rebind', I can't find any reference to it in the chapter
on containers. Assume you can, rely upon... I wouldn't.
For example I tried the following code which uses a 'wrong' allocator
and was slightly surrised to find it compiles on the three compilers I
tried it on
#include <vector>
#include <memory>
int main()
{
std::vector<int , std::allocator< double> > vec;
vec.push_back(1 );
}


V


As a corrolary I tried the following

#include <iostream>
#include <vector>
#include <memory>

typedef std::vector<int , std::allocator< double> >::allocator_ty pe X;

void test(std::alloc ator<double> const&)
{
std::cout << "double\n";
}

void test(std::alloc ator<int> const&)
{
std::cout << "int\n";
}

int main()
{
test(X());
}

What would you expect the output to be, 'int' or 'double'? Both compilers
I tried it on the output was 'int'. Which means those versions of the STL
are rebinding the allocator to be an int allocator.

john
Jul 22 '05 #3

"John Harrison" <jo************ *@hotmail.com> wrote in message
news:opsavy7sdb 212331@andronic us...
If you specify an allocator in an STL container is it a requirement that the allocator allocates object of the right type, or can you assume that the container will rebind the allocator to the correct type?


I think this is addressed by 23.1 [lib.container.r equirements] para.
8:

Copy constructors for all container types defined in this clause copy
an allocator argument from their
respective first parameters. All other constructors for these
container types take an Allocator& argument
(20.1.5), an allocator whose value type is the same as the container's
value type.

Jonathan
Jul 22 '05 #4
"John Harrison" <jo************ *@hotmail.com> wrote in message
news:opsavy7sdb 212331@andronic us...
If you specify an allocator in an STL container is it a requirement that
the allocator allocates object of the right type,
Yes, according to the C++ Standard.
or can you assume that
the container will rebind the allocator to the correct type?


Yes, according to widespread practice.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
Jul 22 '05 #5
On Fri, 09 Jul 2004 20:47:16 GMT, P.J. Plauger <pj*@dinkumware .com> wrote:
"John Harrison" <jo************ *@hotmail.com> wrote in message
news:opsavy7sdb 212331@andronic us...
If you specify an allocator in an STL container is it a requirement that
the allocator allocates object of the right type,


Yes, according to the C++ Standard.
or can you assume that
the container will rebind the allocator to the correct type?


Yes, according to widespread practice.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com


Yes it seems so, both implementations of the STL I've checked do rebind
the allocator for vector at least. This seems to directly contradict the
requirements for allocator_type.

I also noticed that Josuttis' book also ignores what the standard says,
see first page of chapter 15 where he happily passes the same allocator to
several containers with different value types.

What's the reason for the standard saying what it does? It seems a bit
inconvenient.

john
Jul 22 '05 #6

"John Harrison" <jo************ *@hotmail.com> wrote in message
news:opsav1xfa5 212331@andronic us...
On Fri, 09 Jul 2004 20:47:16 GMT, P.J. Plauger <pj*@dinkumware .com> wrote:
"John Harrison" <jo************ *@hotmail.com> wrote in message
news:opsavy7sdb 212331@andronic us...
If you specify an allocator in an STL container is it a requirement that the allocator allocates object of the right type,
Yes, according to the C++ Standard.
or can you assume that the container will rebind the allocator to the correct type?


Yes, according to widespread practice.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com


Yes it seems so, both implementations of the STL I've checked do

rebind the allocator for vector at least. This seems to directly contradict the requirements for allocator_type.
How does this contradict the allocator requirements?
I also noticed that Josuttis' book also ignores what the standard says, see first page of chapter 15 where he happily passes the same allocator to several containers with different value types.


It looks to me like the allocators he uses all have value_types
appropriate for the containers.

Jonathan
Jul 22 '05 #7
On Fri, 9 Jul 2004 15:21:25 -0600, Jonathan Turkanis
<te******@kanga roologic.com> wrote:

"John Harrison" <jo************ *@hotmail.com> wrote in message
news:opsav1xfa5 212331@andronic us...
On Fri, 09 Jul 2004 20:47:16 GMT, P.J. Plauger <pj*@dinkumware .com> wrote:
> "John Harrison" <jo************ *@hotmail.com> wrote in message
> news:opsavy7sdb 212331@andronic us...
>
>> If you specify an allocator in an STL container is it a requirement that >> the allocator allocates object of the right type,
>
> Yes, according to the C++ Standard.
>
>> or can you assume that >> the container will rebind the allocator to the correct type?
>
> Yes, according to widespread practice.
>
> P.J. Plauger
> Dinkumware, Ltd.
> http://www.dinkumware.com
>


Yes it seems so, both implementations of the STL I've checked do

rebind
the allocator for vector at least. This seems to directly contradict

the
requirements for allocator_type.


How does this contradict the allocator requirements?


Not the allocator requirements, the requirements for allocator_type. For
instance 23.2.4 has

template <class T, class Allocator = std::allocator< T> >
class vector
{
...
typedef Allocator allocator_type;

i.e. allocator_type should be the same type as the template parameter.
I also noticed that Josuttis' book also ignores what the standard says,
see first page of chapter 15 where he happily passes the same

allocator to
several containers with different value types.


It looks to me like the allocators he uses all have value_types
appropriate for the containers.


Well maybe I'm reading too much into this but in chpater 15 in quick
succession he gives

vector<int,Spec ialAlloc> v;

map<int,float,l ess<int>,Specia lAlloc> m;

basic_string<ch ar,char_traits< char>,SpecialAl loc> s;

Its not specified anywhere what SpecialAlloc is but by the quote you made
from the standard it cannot have the correct value type for all these
different containers.

john
Jonathan


Jul 22 '05 #8

"John Harrison" <jo************ *@hotmail.com> wrote in message
news:opsav3hke1 212331@andronic us...
On Fri, 9 Jul 2004 15:21:25 -0600, Jonathan Turkanis
<te******@kanga roologic.com> wrote:

"John Harrison" <jo************ *@hotmail.com> wrote in message
news:opsav1xfa5 212331@andronic us...

Yes it seems so, both implementations of the STL I've checked do rebind
the allocator for vector at least. This seems to directly
contradict the
requirements for allocator_type.


How does this contradict the allocator requirements?


Not the allocator requirements,


Sorry, I read your post too fast.
the requirements for allocator_type. For
instance 23.2.4 has

template <class T, class Allocator = std::allocator< T> >
class vector
{
...
typedef Allocator allocator_type;

i.e. allocator_type should be the same type as the template

parameter.

I still don't see the violation.
I also noticed that Josuttis' book also ignores what the standard

says,
see first page of chapter 15 where he happily passes the same

allocator to
several containers with different value types.


It looks to me like the allocators he uses all have value_types
appropriate for the containers.


Well maybe I'm reading too much into this but in chpater 15 in quick
succession he gives

vector<int,Spec ialAlloc> v;

map<int,float,l ess<int>,Specia lAlloc> m;

basic_string<ch ar,char_traits< char>,SpecialAl loc> s;


That's funny. My copy has

vector<int, MyAlloc<int> > v;
map<int,float,l ess<int>,MyAllo c<std::pair<con st int, float> > > m;
...

Jonathan
Jul 22 '05 #9
"John Harrison" <jo************ *@hotmail.com> wrote in message
news:opsav1xfa5 212331@andronic us...
On Fri, 09 Jul 2004 20:47:16 GMT, P.J. Plauger <pj*@dinkumware .com> wrote:
"John Harrison" <jo************ *@hotmail.com> wrote in message
news:opsavy7sdb 212331@andronic us...
If you specify an allocator in an STL container is it a requirement that the allocator allocates object of the right type,
Yes, according to the C++ Standard.
or can you assume that
the container will rebind the allocator to the correct type?


Yes, according to widespread practice.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com


Yes it seems so, both implementations of the STL I've checked do rebind
the allocator for vector at least. This seems to directly contradict the
requirements for allocator_type.


I think this approach gives meaning to an otherwise ill formed program,
hence it's a conforming implementation.
I also noticed that Josuttis' book also ignores what the standard says,
see first page of chapter 15 where he happily passes the same allocator to
several containers with different value types.

What's the reason for the standard saying what it does? It seems a bit
inconvenient.


It was simple, and the rebind mechanism was introduced as a cutesy
trick before template template was well established in the language.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
Jul 22 '05 #10

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

Similar topics

3
1880
by: Bernhard Kick | last post by:
Hi all, I saw this code in the book "Accelerated C++" (chapt 11, iirc): template <class T> class Vec { ... std::allocator<T> alloc; // object to handle memory allocation // ??would static member work here??: // static std::allocator<T> alloc;
3
1533
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> class node { public:
15
4277
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
4134
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...
7
3081
by: Grahamo | last post by:
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
1
2804
by: xqxu.pzhou | last post by:
I wrote a simple allocator "myAlloc" under the g++ 3.2.3. When it is used by Vector, it works well. But when it is used by List, the codes have errors when compling. the error message is: "no matching function for call to myAlloc<std::_List_node<int> Then I added the following copy constructor to "myAlloc", now it works well. "template <typename U> myAlloc(const myAlloc<U>&) {}"
1
2063
by: Chris Thomasson | last post by:
This region allocator is not dynamic and can be fed with a buffer residing on the stack of the calling thread. Therefore, you can use this in an environment which does not have a heap; its basically analogous to `alloca'. However, it does not align each buffer on a boundary sufficient for _any_ type. Instead, it dynamically calculates the types alignment requirements and mutates the offset accordingly. This makes it more efficient wrt...
6
13861
by: Juha Nieminen | last post by:
I tested the speed of a simple program like this: //------------------------------------------------------------ #include <list> #include <boost/pool/pool_alloc.hpp> int main() { typedef std::list<intList_t; // default allocator //typedef std::list<int, boost::pool_allocator<int List_t;
16
2703
by: PeterAPIIT | last post by:
Hello all C++ expert programmer, i have wrote partial general allocator for my container. After reading standard C++ library and code guru article, i have several questions. 1. Why allocator write forward declaration then allocation for void* rather than directly wrote allocator first ?
0
9666
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
9512
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10413
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
10200
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
7530
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
5422
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...
1
4094
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
2
3707
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2909
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.