简体   繁体   中英

Does the C++ linked list (std::list) use new, in relation to a memory heap?

Using Visual Studio for c++; does std::list use new to allocate nodes? I ask because I'm coding a memory heap as a challenge and, if it uses new, that reduces the effectiveness of the memory heap.

Memory heap as per this question, first answer: How to implement a memory heap

If it does use new, how would I fix it (in relation to using the linked list for the memory heap outlined in the answer to the above question)?

Thanks.

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.

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM