[Dune] [Dune-Commit] dune-common r5882 - trunk/dune/common

Oliver Sander sander at mi.fu-berlin.de
Tue Feb 2 22:35:30 CET 2010


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;
> }
>
>   


-- 
************************************************************************
* Oliver Sander                ** email: sander at mi.fu-berlin.de        *
* Freie Universität Berlin     ** phone: + 49 (30) 838 75348           *
* Institut für Mathematik      ** URL  : page.mi.fu-berlin.de/~sander  *
* Arnimallee 6                 ** -------------------------------------*
* 14195 Berlin, Germany        ** Member of MATHEON (www.matheon.de)   *
************************************************************************





More information about the Dune mailing list