版本1和10间的区别 (跳过第9版)
于2006-03-03 14:27:15修订的的版本1
大小: 3317
编辑: czk
备注:
于2008-02-23 15:37:07修订的的版本10
大小: 3245
编辑: localhost
备注: converted to 1.6 markup
删除的内容标记成这样。 加入的内容标记成这样。
行号 1: 行号 1:
[[TableOfContents]] ## page was renamed from 内存分配器
<<TableOfContents>>
行号 3: 行号 4:
= Allocators = = 内存分配器 =
行号 5: 行号 6:
== Summary == == 概述 ==
行号 7: 行号 8:
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. 内存分配器Allocator把内存分配和释放封装起来。它们提供了一个底层的接口,允许有效的分配很多小对象。不同的内存分配器类型表示不同的内存管理策略。
行号 9: 行号 10:
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. 注意,内存分配其只是简单的分配和释放内存,而不是创建和释放对象。STL中还包括一组底层的算法,用来操作为初始化的内存。
行号 11: 行号 12:
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] 注意,内存分配器也不会试图包装多种内存模型。C++之定义了一种内存模型(比如说,两个指针的差总是ptrdiff_t类型),这个内存模型是内存分配器唯一支持的内存模型。这是现在的STL与一开始的STL在内存分配器定义上的一个重大区别。[1]
行号 13: 行号 14:
== Description == == 详述 Description ==
行号 15: 行号 16:
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. 内存分配器接口的细节仍然可能会改变,我们不能保证一些成员函数在将来的版本中还将保留。你应该把分配器看成一个黑盒子"black box"。也就是说,你可以通过指定一个特定的内存分配器[2]为容器的模板参数来选择容器的内存分配策略,但是你不应该对容器是如何使用这个分配器的作任何假设。
行号 17: 行号 18:
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.
可以使用的内存分配器有以下这些。大多数情况下,你不需要关心它们之间的区别:默认的分配器alloc一般都是最好的选择。
行号 23: 行号 20:
== Examples == ||alloc||这是默认的内存分配器。它是线程安全的,一般来说也是性能最好的。||
||pthread_alloc||这是另一个线程安全的内存分配器,它为每一个线程实用不同的内存池。你只能够在提供pthreads的操作系统上使用这个分配器。pthread_alloc一般比alloc更快,尤其是在多处理器的系统上。但是它容易导致资源碎片,一个线程释放出来的内存不能够被其他线程使用。||
||single_client_alloc||快速但不是线程安全的内存分配器。如果程序只使用一个线程,这个分配器可能会比alloc快一点。||
||malloc_alloc||使用标准的库函数malloc的内存分配器。它是线程安全的,但是比较慢。有时候要使用这个分配器的原因是在调试时你可以从边界检查或者内存泄露检查工具里得到更多的信息。||

== 例子 Examples ==
行号 29: 行号 31:
== Concepts == == 概念 Concepts ==
行号 33: 行号 35:
== Types == == 类型 Types ==
行号 41: 行号 43:
== Functions == == 函数 Functions ==
行号 51: 行号 53:
== Notes == == 备注 Notes ==
行号 53: 行号 55:
[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. [1] 做这个改变的原因是,新的接口可以减少内存碎片,并且能够实现出高效的并且线程安全的内存分配器。
行号 55: 行号 57:
[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. [2] 不同的容器可以使用不同的内存分配器。比如说,你可以在某些容器上使用默认的分配器alloc而在其它的容器上使用pthread_alloc。注意,vector<int>和vector<int, pthread_alloc>是不同的类型。

内存分配器

1. 概述

内存分配器Allocator把内存分配和释放封装起来。它们提供了一个底层的接口,允许有效的分配很多小对象。不同的内存分配器类型表示不同的内存管理策略。

注意,内存分配其只是简单的分配和释放内存,而不是创建和释放对象。STL中还包括一组底层的算法,用来操作为初始化的内存。

注意,内存分配器也不会试图包装多种内存模型。C++之定义了一种内存模型(比如说,两个指针的差总是ptrdiff_t类型),这个内存模型是内存分配器唯一支持的内存模型。这是现在的STL与一开始的STL在内存分配器定义上的一个重大区别。[1]

2. 详述 Description

内存分配器接口的细节仍然可能会改变,我们不能保证一些成员函数在将来的版本中还将保留。你应该把分配器看成一个黑盒子"black box"。也就是说,你可以通过指定一个特定的内存分配器[2]为容器的模板参数来选择容器的内存分配策略,但是你不应该对容器是如何使用这个分配器的作任何假设。

可以使用的内存分配器有以下这些。大多数情况下,你不需要关心它们之间的区别:默认的分配器alloc一般都是最好的选择。

alloc

这是默认的内存分配器。它是线程安全的,一般来说也是性能最好的。

pthread_alloc

这是另一个线程安全的内存分配器,它为每一个线程实用不同的内存池。你只能够在提供pthreads的操作系统上使用这个分配器。pthread_alloc一般比alloc更快,尤其是在多处理器的系统上。但是它容易导致资源碎片,一个线程释放出来的内存不能够被其他线程使用。

single_client_alloc

快速但不是线程安全的内存分配器。如果程序只使用一个线程,这个分配器可能会比alloc快一点。

malloc_alloc

使用标准的库函数malloc的内存分配器。它是线程安全的,但是比较慢。有时候要使用这个分配器的原因是在调试时你可以从边界检查或者内存泄露检查工具里得到更多的信息。

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] 做这个改变的原因是,新的接口可以减少内存碎片,并且能够实现出高效的并且线程安全的内存分配器。

[2] 不同的容器可以使用不同的内存分配器。比如说,你可以在某些容器上使用默认的分配器alloc而在其它的容器上使用pthread_alloc。注意,vector<int>和vector<int, pthread_alloc>是不同的类型。

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

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