Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the default allocator GCC uses for STL?

Tags:

c++

gcc

According to this link, gcc provides lots of interesting memory allocators to be used with STL containers, but which is used by default if I don't specify one when creating a std::list?

like image 259
lvella Avatar asked Jan 19 '23 13:01

lvella


2 Answers

As it says on that page you link to,

The current default choice for allocator is __gnu_cxx::new_allocator.

I.e, the default allocator is basically just operator new.

like image 160
Ernest Friedman-Hill Avatar answered Jan 29 '23 11:01

Ernest Friedman-Hill


As per wiki :"The default allocator uses operator new to allocate memory.[13] This is often implemented as a thin layer around the C heap allocation functions,[14] which are usually optimized for infrequent allocation of large memory blocks"

from "ISO/IEC (2003). ISO/IEC 14882:2003(E): Programming Languages - C++" (wiki reference)

Default Allocator:

namespace std {   
  template <class T> class allocator;  
  // specialize for void: template <> class allocator<void>   
  {  
   public:  
   typedef void*    pointer;   
   typedef const void* const_pointer;
   // reference-to-void members are impossible. typedef void value_type;  
   template <class U> struct rebind  {  typedef allocator<U> other;  };  
};


template <class T> class allocator  
{  
public:  
  typedef size_t size_type;  
  typedef ptrdiff_t difference_type;  
  typedef T* pointer;  
  typedef const T* const_pointer;  
  typedef T& reference;  
  typedef const T& const_reference;  
  typedef T template value_type;  
  template <class U> struct rebind { typedef allocator<U> other;   
}; 

  allocator() throw();  
  allocator(const allocator&) throw();  
  template <class U> allocator(const allocator<U>&) throw();  
  ̃allocator() throw();
   pointer address(reference x) const;      
  const_pointer address(const_reference x) const;`    

  pointer allocate(  
     size_type, allocator<void>::const_pointer hint = 0);  
     void deallocate(pointer p, size_type n);  
     size_type max_size() const throw();  
     void construct(pointer p, const T& val);  
     void destroy(pointer p);  
     };  
}
like image 37
Tatvamasi Avatar answered Jan 29 '23 12:01

Tatvamasi