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

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 1913
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.********@comAcast.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_type X;

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

void test(std::allocator<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:opsavy7sdb212331@andronicus...
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.requirements] 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:opsavy7sdb212331@andronicus...
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:opsavy7sdb212331@andronicus...
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:opsav1xfa5212331@andronicus...
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:opsavy7sdb212331@andronicus...
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******@kangaroologic.com> wrote:

"John Harrison" <jo*************@hotmail.com> wrote in message
news:opsav1xfa5212331@andronicus...
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:opsavy7sdb212331@andronicus...
>
>> 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,SpecialAlloc> v;

map<int,float,less<int>,SpecialAlloc> m;

basic_string<char,char_traits<char>,SpecialAlloc> 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:opsav3hke1212331@andronicus...
On Fri, 9 Jul 2004 15:21:25 -0600, Jonathan Turkanis
<te******@kangaroologic.com> wrote:

"John Harrison" <jo*************@hotmail.com> wrote in message
news:opsav1xfa5212331@andronicus...

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,SpecialAlloc> v;

map<int,float,less<int>,SpecialAlloc> m;

basic_string<char,char_traits<char>,SpecialAlloc> s;


That's funny. My copy has

vector<int, MyAlloc<int> > v;
map<int,float,less<int>,MyAlloc<std::pair<const int, float> > > m;
...

Jonathan
Jul 22 '05 #9
"John Harrison" <jo*************@hotmail.com> wrote in message
news:opsav1xfa5212331@andronicus...
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:opsavy7sdb212331@andronicus...
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
Jonathan Turkanis wrote:
"John Harrison" <jo*************@hotmail.com> wrote in message
news:opsav3hke1212331@andronicus...
On Fri, 9 Jul 2004 15:21:25 -0600, Jonathan Turkanis
<te******@kangaroologic.com> wrote:

"John Harrison" <jo*************@hotmail.com> wrote in message
news:opsav1xfa5212331@andronicus... [...]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,SpecialAlloc> v;

map<int,float,less<int>,SpecialAlloc> m;

basic_string<char,char_traits<char>,SpecialAlloc > s;

That's funny. My copy has

vector<int, MyAlloc<int> > v;
map<int,float,less<int>,MyAlloc<std::pair<const int, float> > > m;
...


<Butting in...>

So does mine, but it's the 7th printing (what's yours?) so, either
before they printed something differently or after...

V
Jul 22 '05 #11

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:uv*****************@ord-read.news.verio.net...
Jonathan Turkanis wrote:
"John Harrison" <jo*************@hotmail.com> wrote in message
news:opsav3hke1212331@andronicus...
Well maybe I'm reading too much into this but in chpater 15 in quicksuccession he gives

vector<int,SpecialAlloc> v;

map<int,float,less<int>,SpecialAlloc> m;

basic_string<char,char_traits<char>,SpecialAlloc > s;

That's funny. My copy has

vector<int, MyAlloc<int> > v;
map<int,float,less<int>,MyAlloc<std::pair<const int, float> > > m; ...


<Butting in...>

So does mine, but it's the 7th printing (what's yours?) so, either
before they printed something differently or after...


Mine is the 10th. I wonder if Josuttis changed it because he decided
it was an error or because he thought it was debatable or an
unnecessary distraction.

I'm a bit suprised that the requirement that the value_type's match is
stated clearly for basic_string (21.3/1) but only in an almost
parenthetical way for containers generally (23.1/8), AFAICT.

Jonathan
Jul 22 '05 #12
>>
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.


Yes, thinking about it I agree.

john
Jul 22 '05 #13
On Fri, 9 Jul 2004 16:13:19 -0600, Jonathan Turkanis
<te******@kangaroologic.com> wrote:

Mine is the 10th. I wonder if Josuttis changed it because he decided
it was an error or because he thought it was debatable or an
unnecessary distraction.


Mine's the third, the change is listed in the errata on his website.

john
Jul 22 '05 #14

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

Similar topics

3
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...
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>...
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...
7
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...
1
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...
1
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...
6
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...
16
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...
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
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...

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.