1

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.

Community
  • 1
  • 1
Narf the Mouse
  • 1,541
  • 5
  • 18
  • 30
  • Yes it does. Also, you can profile it and see it for yourself. – Eitan T Oct 24 '12 at 19:47
  • duplicate of http://stackoverflow.com/questions/8882442/list-and-list-elements-where-are-stored – amdn Oct 24 '12 at 19:47
  • How it uses `new` is, of course, implementation dependent, but it must be using *some* allocator *somewhere*, and the implementations `new` should be at the root of those. *//starts waiting for someone to point out a real life odd-ball cases where it isn't* – dmckee --- ex-moderator kitten Oct 24 '12 at 19:47
  • Not a duplicate; that question does not address a memory heap. – Narf the Mouse Oct 24 '12 at 19:51
  • Why does operator new ruin your heap? Why not override new and delete and have them use your heap implementation? – Peter Ruderman Oct 24 '12 at 19:53
  • @Narf: But [this answer](http://stackoverflow.com/a/8882738/103167) on that question describes allocation. Dupe doesn't mean exactly equivalent, it means your question overlaps the other to the point where the answers are the same. – Ben Voigt Oct 24 '12 at 19:53
  • @PeterRuderman: While replacing global `operator new` is permitted (with the exception of placement `new`), it's a global solution to a local problem, and may have unexpected side effects. And defining `operator new` for the list element type isn't sufficient, because `std::list` typically also allocates metadata structures. – Ben Voigt Oct 24 '12 at 19:55
  • @Ben Voigt: You're assuming that he's trying to solve a local problem. It sounds to me more like he wants to replace all memory allocation with his custom heap. It's not clear from the question, hence my follow up. – Peter Ruderman Oct 24 '12 at 19:59
  • @Peter: Since this is just me figuring out C++, either is good, at this point. Also, wouldn't allocating from a memory heap to allocate from a heap be a circular reference? – Narf the Mouse Oct 24 '12 at 20:03
  • @Ben Voigt: And questions like that are why it's not a duplicate. – Narf the Mouse Oct 24 '12 at 20:05
  • @Narf the Mouse: C++ allows you to replace the new and delete operators with your own custom implementations. Your version of new and delete could grab memory from your custom heap. If your heap is implemented using new and delete, that would obviously be a circular reference and seriously bad mojo. ;) You can avoid that by using OS APIs to get your big memory blocks (such as HeapAlloc on Win32) or using the old C functions malloc and free. – Peter Ruderman Oct 24 '12 at 20:07
  • @Narf: No, details like that illustrate the difference between *the question you asked* and *the problem you're trying to solve*. You might have meant "How do I force all allocations in my program to come from my custom heap?", but you asked "Does `std::list` use `new`?" – Ben Voigt Oct 24 '12 at 20:07
  • @Ben Voigt: "If it does use new, how would I fix it (using the memory heap outlined in the answer)?" I suppose there's a disconnect between the title and the body of the question. – Narf the Mouse Oct 24 '12 at 20:11
  • @Narf: That still suggests you want a particular `std::list`, and nothing else, to use your heap. What Peter has suggested will change every `std::list`, and everything else, to use your heap, which is an answer to a different question. – Ben Voigt Oct 24 '12 at 20:13
  • @Peter Ruderman: Thanks, That should work. – Narf the Mouse Oct 24 '12 at 20:13
  • @Ben Voigt: You have a point there. – Narf the Mouse Oct 24 '12 at 20:15

4 Answers4

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.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
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.

Chad
  • 18,706
  • 4
  • 46
  • 63
  • To be pedantic, the containers use an allocator, and `std::allocator` is the default. But it's not required that the allocator be `std::allocator`. – Ben Voigt Oct 24 '12 at 19:50
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.

K-ballo
  • 80,396
  • 20
  • 159
  • 169
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.

user1610015
  • 6,561
  • 2
  • 15
  • 18