2011-06-10 44 views
4

根據this的鏈接,gcc提供了很多有趣的內存分配器來與STL容器一起使用,但是如果我在創建std :: list時沒有指定一個,默認情況下會使用它?GCC用於STL的默認分配器是什麼?

+0

我想象編譯器不會做關於內存分配器的_anything_;我期望它將所有的內存分配給鏈接器/加載器和運行庫'libc'和'libstdC++'庫。 – sarnold 2011-06-10 02:09:18

回答

6

,因爲它說你鏈接到網頁上,

當前默認的選擇。分配器是__gnu_cxx :: new_allocator。

即,默認分配器基本上只是operator new

+0

我想我錯過了它......謝謝 – lvella 2011-06-10 02:46:47

+0

它實際上取決於一個可以傳遞給GCC的配置腳本(--enable-libstdcxx-allocator)的選項,所以你的答案不一定是正確的。 – Fanael 2011-06-10 16:23:06

3

作爲每維基:「的默認分配器使用操作者新分配內存[13]這通常實現爲圍繞C堆中分配函數的薄層,[14],其通常用於大不頻繁分配優化。存儲器塊」

從 「ISO/IEC(2003)ISO/IEC 14882:2003(E):編程語言 - C++」(維基參考)

默認分配器:

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); 
    }; 
} 
+0

不正確?投票原因? – Tatvamasi 2011-06-10 02:38:04

+0

+1反擊不公平的反對票。 – OneOfOne 2011-06-10 02:39:52

+0

我認爲,因爲這個問題是關於'gcc'的具體問題,而且因爲你的答案是按照標準來說的,所以它並不完全回答這個問題。 – 2011-06-10 02:40:37