boost::array style fixed-size template data containers

Started by
10 comments, last by maximAL 15 years, 9 months ago
Quote:Original post by maximAL
in my experience, it's normal to use some #define/constant to allocate some fixed-size array.


That is something else entirely. Since C does not have a dynamic array implementation, it's often easier to implement a dynamic array as a subset of a large static array, than it is to rewrite the realloc-based code to make the array truly dynamic. In this approach, a dynamic array is represented as a size and a large static array (where the elements at the end of the static array are not in the abstract dynamic array, and their value is thus not important).

This, however, is difficult in C++ (because it is not idiomatic to have useless initialized objects at the end of an array), which is why using a vector that's initially resized to a large value is a good alternative.

Quote:
Quote:Original post by Zahlman
But anyway, template code generation is not quite as dumb as that description suggests.

thats basically the question. could you elaborate on that?


Many linkers support the ability to collapse together identical templated code. Thus, you could have a hundred different template instances, the code for a function such as "return the first element" would still appear once in the executable.

Quote:not how many items, the maximum number of items. when working with tight memory limitations you should know the maximum memory usage beforehand anyways, as you can't afford running out of memory because of a situation that didn't occur in your testing.
and a list still has completely other access characteristics than an array. in my experience, developers resort to plain arrays even if other containers might be faster simply because of allocation issues.


The maximum number of items is an entirely different story, and one which can be elegantly solved with allocators while keeping the existing container implementations.

Advertisement
so, i had a look into allocators again and read this little tutorial.
seems rather straight forward, but i encountered a little problem with the allocate function:
pointer allocate(size_type n, const_pointer = 0)

the function is supposed to return an pointer to a contiguous memory block for n objects.
now, when i base the allocator on a simple fixed array, i run into fragmentation issues again, as there may be enough elements, but not in a contiguous block.
at the moment i'm not sure what to make of this. maybe i could assume that containers besides vectors and strings won't use n > 1 and introduce some extra handling or limitations on the two aforementioned...

[Edited by - maximAL on July 19, 2008 5:34:45 PM]
------------------------------------------------------------Jawohl, Herr Oberst!

This topic is closed to new replies.

Advertisement