[Dune] [Dune-Commit] dune-common r5882 - trunk/dune/common
Carsten Gräser
graeser at math.fu-berlin.de
Tue Feb 2 23:32:36 CET 2010
Hi Oliver,
if you want to use this allocator for the functionality of ReservedVector
you would still need to a new class that e.g. derives from std::vector<T,array_alloc>
and also encapsulates the static storage.
Then you would need to pass the storage to a temp allocator and this
one to the base class constructor. (Can you pass a member to a base
class constructor?)
Furthermore you have at least the overhead of the pointer and size of the
reserved memory in the std::vector that is not necessary in this case.
Although reimplementing a full vector seems not no be nice I'm not sure
whether such an allocator would be a better solution.
Regards
Carsten
Am 02.02.2010 22:35, schrieb Oliver Sander:
> Hi Christian!
> I am afraid I don't really understand what your example code is
> supposed to tell me, but that is probably me and the hour.
> However I guess whatever it does it cannot work because you
> never allocate memory for the array_allocator. I think you need
> something like
>
> typedef __gnu_cxx::array_allocator<int, std::tr1::array<int,2> >
> MyAllocator;
> std::tr1::array<int,2> myMemory; // this is where the actual memory is
> MyAllocator myAllocator(&myMemory); // and the allocator takes the
> memory
> std::vector<int,MyAllocator> foo3(myAllocator);
>
> Okay, four lines just for an array isn't the ultimate in elegance, but
> note that I can replace the last line by
>
> std::set<int,MyAllocator> foo3(myAllocator);
>
> and get a set with static memory.
>>
>>
>> Consider the code below. It will not work. And despite the fact that
>> the two approaches do something different, I don't think that it is
>> easier to maintain some code copied from the gcc, that this rather
>> simple well documented file.
>>
> In my implementation the gcc code is shorter than reservedvector.h and it
> doesn't seem to contain any evil tricks. It is not as well documented as
> your code, but I would volunteer to change that.
>
> :-)
> Oliver
>
>
>> You might say that we could still write some other kind of allocator,
>> but I really have no idea, how this allocator should work.
>>
>> Christian
>>
>> Example:
>>
>> #ifdef HAVE_CONFIG_H
>> #include "config.h"
>> #endif
>> #include <dune/common/fvector.hh>
>> #include <dune/common/exceptions.hh>
>> #include <iostream>
>>
>> #include <vector>
>> #include <ext/array_allocator.h>
>> #include <dune/common/reservedvector.hh>
>>
>> int main()
>> {
>> Dune::ReservedVector<int,2> foo[2];
>> typedef __gnu_cxx::array_allocator<int> alloc;
>> std::vector<int,alloc> foo2[2];
>>
>> char * oldptr = 0;
>> char * oldptr2 = 0;
>> for (int i=0; i<2; i++)
>> for (int j=0; j<2; j++)
>> {
>> std::cout << &(foo[i][j]) << "\t";
>> std::cout << ((char*)&(foo[i][j]))-oldptr << "\t";
>> std::cout << &(foo2[i][j]) << "\t";
>> std::cout << ((char*)&(foo2[i][j]))-oldptr2 << std::endl;
>> oldptr = (char*)&(foo[i][j]);
>> oldptr2 = (char*)&(foo2[i][j]);
>> }
>>
>> return 0;
>> }
>>
>>
>
>
--
----------------------------------------------------------------------
Carsten Gräser | phone: +49-30 / 838-75349
Freie Universität Berlin | fax : +49-30 / 838-54977
Institut für Mathematik | email: graeser at math.fu-berlin.de
Arnimallee 6 |
14195 Berlin, Germany | URL : http://page.mi.fu-berlin.de/graeser
----------------------------------------------------------------------
More information about the Dune
mailing list