版本1和2间的区别
于2006-03-03 14:27:15修订的的版本1
大小: 3317
编辑: czk
备注:
于2006-03-03 14:55:44修订的的版本2
大小: 3382
编辑: czk
备注:
删除的内容标记成这样。 加入的内容标记成这样。
行号 3: 行号 3:
= Allocators = = 内存分配器 Allocators =
行号 5: 行号 5:
== Summary == == 概述 Summary ==
行号 13: 行号 13:
== Description == == 详述 Description ==
行号 23: 行号 23:
== Examples == == 例子 Examples ==
行号 29: 行号 29:
== Concepts == == 概念 Concepts ==
行号 33: 行号 33:
== Types == == 类型 Types ==
行号 41: 行号 41:
== Functions == == 函数 Functions ==
行号 51: 行号 51:
== Notes == == 备注 Notes ==

TableOfContents

内存分配器 Allocators

1. 概述 Summary

Allocators encapsulate allocation and deallocation of memory. They provide a low-level interface that permits efficient allocation of many small objects; different allocator types represent different schemes for memory management.

Note that allocators simply allocate and deallocate memory, as opposed to creating and destroying objects. The STL also includes several low-level algorithms for manipulating uninitialized memory.

Note also that allocators do not attempt to encapsulate multiple memory models. The C++ language only defines a single memory model (the difference of two pointers, for example, is always ptrdiff_t), and this memory model is the only one that allocators support. This is a major change from the definition of allocators in the original STL. [1]

2. 详述 Description

The details of the allocator interface are still subject to change, and we do not guarantee that specific member functions will remain in future versions. You should think of an allocator as a "black box". That is, you may select a container's memory allocation strategy by instantiating the container template with a particular allocator [2], but you should not make any assumptions about how the container actually uses the allocator.

The available allocators are as follows. In most cases you shouldn't have to worry about the distinction: the default allocator, alloc, is usually the best choice. alloc The default allocator. It is thread-safe, and usually has the best performance characteristics. pthread_alloc A thread-safe allocator that uses a different memory pool for each thread; you can only use pthread_alloc if your operating system provides pthreads. Pthread_alloc is usually faster than alloc, especially on multiprocessor systems. It can, however, cause resource fragmentation: memory deallocated in one thread is not available for use by other threads. single_client_alloc A fast but thread-unsafe allocator. In programs that only have one thread, this allocator might be faster than alloc. malloc_alloc An allocator that simply uses the standard library function malloc. It is thread-safe but slow; the main reason why you might sometimes want to use it is to get more useful information from bounds-checking or leak-detection tools while you are debugging.

3. 例子 Examples

   1 vector<double> V(100, 5.0);     // Uses the default allocator.
   2 vector<double, single_client_alloc> local(V.begin(), V.end());

4. 概念 Concepts

  • Allocator

5. 类型 Types

  • alloc
  • pthread_alloc
  • single_client_alloc
  • malloc_alloc
  • raw_storage_iterator

6. 函数 Functions

  • construct
  • destroy
  • uninitialized_copy
  • uninitialized_fill
  • uninitialized_fill_n
  • get_temporary_buffer
  • return_temporary_buffer

7. 备注 Notes

[1] The reason for this change is that the new interface reduces memory fragmentation, and that it allows an implementation that is both efficient and thread-safe.

[2] Different containers may use different allocators. You might, for example, have some containers that use the default allocator alloc and others that use pthread_alloc. Note, however, that vector<int> and vector<int, pthread_alloc> are distinct types.

STL编程指南/内存分配器 (2008-02-23 15:37:07由localhost编辑)

ch3n2k.com | Copyright (c) 2004-2020 czk.