1

使用 Visual Studio for c++;std::list 是否使用 new 来分配节点?我问是因为我将内存堆编码为一项挑战,如果它使用新的,则会降低内存堆的有效性。

内存堆按照这个问题,第一个答案:How to implement a memory heap

如果它确实使用了新的,我将如何修复它(关于使用链表作为上述问题的答案中概述的内存堆)?

谢谢。

4

4 回答 4

5

STL containers (so-called because the design is based on the STL) in the C++ Standard have a template parameter which specifies an allocator. That allocator is used. It defaults to a library-provided allocator, but you can pass your own that uses your custom heap.

于 2012-10-24T19:46:56.680 回答
4

All standard template library containers use an abstraction (called an Allocator) to allocate memory, the default being a std::allocator<T>. This default allocator does use new, but that doesn't preclude you from using (writing) one that doesn't.

You can see from this documentation that the second template parameter is the allocator to use.

于 2012-10-24T19:46:42.963 回答
2

Yes, it does use new indirectly via its Allocator parameter. You can write a custom allocator that uses your heap, and instantiate lists with it.

于 2012-10-24T19:46:32.697 回答
1

Yes, std::list by default uses std::allocator, which uses new.

But you can write your own allocator class that uses any allocation scheme you want and pass it as the second template argument to std::list.

于 2012-10-24T19:48:01.230 回答