Click here to Skip to main content
15,868,047 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am about to implement a doubly-linked list (the default std::list doesn't fit my needs because I need direct access to the internal nodes). My question is if I should use "std::allocator" or the keyword "new" when implementing it. I understand the power with "allocator" but I don't plan to make any custom allocator, so will it be any different against using "new" in performance?

So to clarify, is there any performance difference between using "std::allocator" or "new".
Posted
Updated 28-Mar-13 14:27pm
v2

1 solution

It's totally up to you. This feature is used to give the user access to allocation method, to provide an alternative allocator. In the code using the container, it could be a sub-allocator or something, used to boost performance, taking into account the particular order of allocation/deallocation, amount of data, etc. For example, a faster sub-allocator technique could improve performance is the container elements allocate tiny amount of memory.

It all depends on your purpose. Generally, if you want to publish you class for a wide audience of the potential users, you would need to introduce custom allocator. If you do it for more special cases, for your own cases you know pretty well, you may want to skip this feature. In this case, after all, you can improve your implementation later, when you face with the need to optimize the use of your collection by using custom allocation/deallocation.

I must note that all structures like linked lists or trees rely on very frequent use of allocation/deallocation, in contrast, say, to containers based on array (more exactly, on bigger continuous chunks of memory). Such structures, by their nature, inherently create high level of fragmentation of memory; the memory allocated by items/nodes can freely interlace with other unrelated object on the heap. That's why custom allocation is more important for such data structures.

—SA
 
Share this answer
 
Comments
WaZoX 28-Mar-13 21:00pm    
Thank you very much! Made things a bit clearer.
Sergey Alexandrovich Kryukov 28-Mar-13 21:02pm    
My pleasure.
Good luck, call again.
—SA
WaZoX 28-Mar-13 21:03pm    
Also found this http://www.codeproject.com/Articles/4795/C-Standard-Allocator-An-Introduction-and-Implement great article for those who also are interested.
Sergey Alexandrovich Kryukov 28-Mar-13 21:12pm    
Well, from the first glance, looks pretty good.
Cheers,
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900