Looking for alternative vector library

Hey there,

the class vector is not exportable via windows dll, so i am looking for a replacement... if you can provide a link to a library with the same api (different namespace is ok) it would be awesome :)
closed account (o1vk4iN6)
What you mean exportable ? You need to give it a type so that it can be compiled into the dll, not sure what you are trying to do but any other vector libraries will also be templated so it won't be "exportable" either.
> the class vector is not exportable via windows dll

Memory allocated in the dll must be deallocatable outside the dll and vice versa. A work around is to write a custom allocator that allocates and frees memory from the common process default heap.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
template < typename T > struct heap_allocator
{
    typedef std::size_t size_type ;
    typedef std::ptrdiff_t  difference_type ;
    typedef T* pointer ;
    typedef const T* const_pointer ;
    typedef T& reference ;
    typedef const T& const_reference ;
    typedef T value_type ;

    template<typename U> struct rebind { typedef heap_allocator<U> other ; } ;

    heap_allocator() throw() /* noexcept C++11 */ {}
    heap_allocator( const heap_allocator& ) throw() {}
    template <typename U> heap_allocator( const heap_allocator<U>& ) throw() {}
    ~heap_allocator() throw() {}

     pointer address( reference r ) const { return &r ; }
     const_pointer address( const_reference r ) const { return &r ; }

     pointer allocate( size_type n, const void* = 0 )
     {
         void* p = ::HeapAlloc( ::GetProcessHeap(),  0, n * sizeof(T) ) ;
         if( p == 0 ) throw std::bad_alloc() ;
         return static_cast<pointer>(p) ;
     }
     void deallocate( pointer p , size_type )
     { ::HeapFree( ::GetProcessHeap(), 0, p ) ; }

     size_type max_size() const throw() { return size_type(-1) / sizeof(T) ; }

     void construct( pointer p, const T& v ) { ::new(p) T(v) ; }

     /*
     template< typename... ARGS > void construct( pointer p, ARGS&&... args )
     { ::new (p) T( std::forward<ARGS>(args...)... ) ; } // C++11
     */

     void destroy( pointer p ) { if(p) p->~T() ; }
};


And now:
1
2
3
4
5
typedef std::basic_string< char, std::char_traits<char>, heap_allocator<char> > string_type ;
typedef std::vector< string_type, heap_allocator<string_type> > vector_type ;
typedef std::list< string_type, heap_allocator<string_type> > list_type ;

vector_type my_dll_function( list_type& lst, string_type& str ) ;


Needless to say, you must compile the dll and the program using the dll with the same compiler, with compatible compiler options.
Last edited on
Topic archived. No new replies allowed.