f



std::list with cutom allocator

{ edited to shorten lines to ~70 characters. -mod }

Hi,
I have a custom allocator which supports only one element per
allocation.
I am trying to making the allocator complaint with the STL containers.

As per the standard, I need to provide allocator::max_size() function,
which should Return the maximum theoretically possible value of n, for
which the call allocate(n, 0) could succeed.

In my case it is 1, as I only support one object per allocation.

However, with that implementation, I'm finding it problematic to use the
allocator with std::list. 

std::list also provides a function std::list::max_size(), which should
return the maximum number of elements the container is able to hold due
to system or library implementation limitations. In theory, this should
not be related with the allocator, but GCC 4.82 has implemented it in
this way.

      /**  Returns the size() of the largest possible %list.  */
      size_type
      max_size() const _GLIBCXX_NOEXCEPT
      { return _M_get_Node_allocator().max_size(); }

And in MSVC12,

	size_type max_size() const _NOEXCEPT
		{	// return maximum possible length of sequence
		return (this->_Getal().max_size());
		}

In my case the std::list::max_size() function returns 1, which I believe
is wrong. Is this a bug in the implementation, or am I missing something
obvious?

Thanks,
Saliya.


-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
hamparawa
11/15/2015 8:05:17 AM
comp.lang.c++.moderated 10738 articles. 1 followers. allnor (8509) is leader. Post Follow

1 Replies
419 Views

Similar Articles

[PageSpeed] 44

Am 15.11.2015 um 15:05 schrieb hamparawa@googlemail.com:
> Hi,
> I have a custom allocator which supports only one element per
> allocation.
> I am trying to making the allocator complaint with the STL containers.
>
> As per the standard, I need to provide allocator::max_size() function,
> which should Return the maximum theoretically possible value of n, for
> which the call allocate(n, 0) could succeed.
>
> In my case it is 1, as I only support one object per allocation.
>
> However, with that implementation, I'm finding it problematic to use
the
> allocator with std::list.
>
> std::list also provides a function std::list::max_size(), which should
> return the maximum number of elements the container is able to hold
due
> to system or library implementation limitations. In theory, this
should
> not be related with the allocator, but GCC 4.82 has implemented it in
> this way.
>
>        /**  Returns the size() of the largest possible %list.  */
>        size_type
>        max_size() const _GLIBCXX_NOEXCEPT
>        { return _M_get_Node_allocator().max_size(); }
>
> And in MSVC12,
>
> 	size_type max_size() const _NOEXCEPT
> 		{	// return maximum possible length of sequence
> 		return (this->_Getal().max_size());
> 		}
>
> In my case the std::list::max_size() function returns 1, which I
believe
> is wrong. Is this a bug in the implementation, or am I missing
something
> obvious?

It doesn't look like an error of the corresponding implementation to me,

because the requirements imposed by the Standard don't seem to be 
specific enough to prevent it to from being an infeasible
implementation.

For a contiguous container (such as std::vector), such a choice seems 
rather reasonable, but given your use-case of a typical node-based 
container such as std::list, this choice seems not very appropriate. I 
see the following options for you:

1) You could make a feature request to your Library vendor(s) to use a 
different implementation of std::list<>::max_size() that would cover 
this kind of use-case as well. You might have luck, when the vendor 
agrees, but in any case this would not be something that you could rely 
on when switching between implementations.

2) You could consider to submit a library issue (as described on 
https://isocpp.org/std/submit-issue when it refers to standard library 
issues), requesting that the standard should at least for some dedicated

container types (such as std::list), specify the semantics of the 
container's max_size() function in a more specific way, that would 
support your example. Since this could me considered as a feature-like 
request, your attempt might be rejected by the committee as being 
not-a-defect (NAD). This strongly depends on the clarity of your issue 
description and argumentation.

3) You could write a proposal (see 
https://isocpp.org/std/submit-a-proposal), that argues why the current 
specification of max_size() at least for some containers should be 
changed to a more specific requirement, that would support examples like

yours. As described on the referenced page, you should start with a new 
discussion on the std-proposals news group 
(https://groups.google.com/a/isocpp.org/forum/?fromgroups#!forum/std-pro
posals).

For any of these options it is always a good idea to have a better 
suggestion how to implement max_size() for e.g. std::list and if so, to 
try to convince implementations to accept such an alternative. You could

for example try to make a contribution to gcc's libstdc++ in regard to 
this. Existing implementations are often a good argumentation base for 
requesting a change of the existing specification.

HTH & Greetings from Bremen,

Daniel Kr�gler



-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
UTF
11/15/2015 2:25:14 PM
Reply:

Similar Artilces:

std::list with custom allocator
Hi, I have a custom allocator which supports only one element per allocation. I= am trying to making the allocator complaint with the STL containers. As per the standard, I need to provide allocator::max_size() function, whic= h should Return the maximum theoretically possible value of n, for which th= e call allocate(n, 0) could succeed. In my case it is 1, as I only support one object per allocation. However, with that implementation, I'm finding it problematic to use the al= locator with std::list.=20 std::list also provides a function std::list::max_size(), which shou...

List of lists of lists of lists...
I would like to have a list of lists N times deep, and my solution is (in pseudocode): def deep(x): a=[x] return a mylist=[] for N: mylist=deep(mylist) Is there a more elegant way to do it? The maine idea is: from a list having the numbre of steps along N dimensions, generate a list with an item at each possible point. Example 1: N=2 list=[2,3] result=[[1,2],[1,2],[1,2]] Example 2: N=3 list=[3,1,2] result=[[[1,2,3]],[[1,2,3]]] -- Ángel Gutiérrez Rodríguez - agr@fq.uniovi.es Instituto de Ciencia de los Materiales de Madrid - CSIC SpLine - European Syncrothorn Radiat...

A solution for a fast std::list::size() *and* a fast std::list::splice()
If I'm not completely mistaken, the only reason why std::list::size() may be (and usually is) a linear-time operation is because they want std::list::splice() to be a constant-time operation, and if you execute the latter, the size of the resulting lists cannot be known without explicitly counting the sizes of the new lists. I was thinking: What if size() was an O(n) operation only *after* a splice() operation has been performed (and only the first time size() is called after that), but O(1) otherwise? In other words, keep a size variable in the list class and update it as necessar...

confusion about algorithm of allocating space, allocate() in alloc.c
hi, I am new here, and I just started to learn lcc with the text "A retargetable C compiler". I am so confused with the algorithm of allocating space stated in Chapter 2 of the text. Following is the source code of the function in V3.6 as well as in V4.2. 53 void *allocate(n, a) unsigned long n; unsigned a; { 54 struct block *ap; 55 56 assert(a < NELEMS(arena)); 57 assert(n > 0); 58 ap = arena[a]; 59 n = roundup(n, sizeof (union align)); 60 while (n > ap->limit - ap->avail) { 61 if ((ap->ne...

std::list custom allocator issues
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 this, and changed the charalloc function to be a regular allocate function. But I am running into problems, which I am thinking may be partly because the alloc...

C-One Mailing List
So, we can get spam by the buckets in the C-One mailing list, but I post = a message about Chuck Peddle and it's moderated as off-topic? <http://blog.paytonbyrd.com/?p=3D74> --=20 Payton Byrd Homepage - <http://www.paytonbyrd.com> Blog - <http://blogs.ittoolbox.com/visualbasic/dotnet/> >>>>> "PB" == Payton Byrd <plbyrd@bellsouth.no.spam.net> writes: PB> So, we can get spam by the buckets in the C-One mailing list, but PB> I post a message about Chuck Peddle and it's moderated as PB> off-topic? This has nothing to do with c...

std::list of C-style arrays
Hello, Could someone explain why the following code is illegal? (I'm trying to use a list of (C-style) arrays.) #include <list> typedef std::list < int[2] > foo_t; int main() { int v[2] = { 12, 34 }; foo_t bar; bar.push_front(v); return 0; } $ g++ -std=c++98 -Wall foo.cxx stl_construct.h: In function `void std::_Construct(_T1*, const _T2&) [with _T1 = int[2], _T2 = int[2]]': stl_list.h:438: instantiated from `std::_List_node<_Tp>* std::list<_Tp, _Alloc>::_M_create_node(const _Tp&) [with _Tp = int[2], _Alloc = std::allocator<int[2]&...

list of lists of lists ....
Hi, I have a list of data (type A) my list can includes element of type A or a lists, these list can includes element of type A or a lists, and so on ... is there a simple way to obtain a single list of all the elemets of type A ? thanks yomgui I forgot the most important, I am looking for a non recursive method. thanks yomgui yomgui wrote: > > Hi, > > I have a list of data (type A) > my list can includes element of type A or a lists, > these list can includes element of type A or a lists, and so on ... > > is there a simple way to obtain a single list of all...

Why C++ Questions on a C List
For those of you who may wonder why so many C++ questions appear on a C newsgroup, I thought I'd let you know of a bizarre experience I had teaching as an adjunct at a local Junior College. The class I was supposed to teach was "C Programming". There was also a "C++ Programming" course which was the follow-on to the C programming course. What surprised me was that in the "C Programming" course they were using a C++ book! That's right -- they were telling people they were teaching them C and in reality they were teaching them C++! It wasn't...

Linked lists in C/C++
Hello guys, I just have a quick question about C++. I am moving from Java to C++. And as many of Java users know, all linked list and trees are already implemented and you just have to instantiate them. Is tehre a simiar thing in c++, or I have to write my own Linked list class? Thank you very much Cheers hana1 wrote: > Hello guys, > I just have a quick question about C++. I am moving from Java to C++. And as > many of Java users know, all linked list and trees are already implemented > and you just have to instantiate them. Is tehre a simiar thing in c++, or I > have...

How to read an input text from keyboard by the std. lang. C?
Hello, how to read an input text from keyboard (on a console) by the standard languace C, please? I really need a tip about this question. TIA, Vinicius. What kind of input? Do you want to read a word? A sentence? A number? There's more than one way to get input from stdin, but for the most part you'll find yourself using fgets: #include <stdio.h> #include <stdlib.h> int main(void) { char buffer[BUFSIZ]; printf("Enter some text: "); fflush(stdout); if (fgets(buffer, sizeof buffer, stdin) == NULL) { fprintf(stderr, "Input error\n"); return EXIT_FAIL...

How to convert from std::list<T*>::iterator to std::list<const T*>::iterator?
Hi, Suppose I have a list which contains pointers. I want the pointer got by dereferencing the iterator be a pointer pointing to a const object. But std::list<const T*>::const_iterator doens't give me this capability. So I want std::list<T*>::iterator. However, the container is of type std::list<T*>. How to get std::list<const T*>::iterator? Best wishes, Peng PengYu.UT@gmail.com wrote: > Suppose I have a list which contains pointers. I want the pointer got > by dereferencing the iterator be a pointer pointing to a const object. Store const pointers. Or ...

jython and C-c C-c
How do I get C-c C-c to work with jython? I have the jpython command set to jython, and I can start the interpreter with C-C ! and then use C-c C-c, but this is rather frustrating. If I try to use C-c C-c without first starting the interpreter in another window I get wrong type argument: sequencep, jpython Thanks, Dave Cook ...

Better C/C++ Than C/C++?
I am looking for a good systems programming language that can be used instead of C/C++. My qualifications for the language are: * mature compiler(s) that produce native-code binaries (for Windows) * open source - preferable, but not 100% necessary Thanks, Kevin "Kevin Albrecht" <kevin@albrecht.net> writes: > I am looking for a good systems programming language > that can be used instead of C/C++. My qualifications > for the language are: > > * mature compiler(s) that produce native-code > binaries (for Windows) Ocaml, D, cyclone, Eiffel, Beta >...

Web resources about - std::list with cutom allocator - comp.lang.c++.moderated

Resources last updated: 1/25/2016 12:15:47 AM