Jump to content
  • Advertisement
Sign in to follow this  
Josheir

C++ Template Allocator Question, Please

This topic is 386 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

The following is an excerpt from C++ Common Knowledge : essential intermediate programming by Stephen C. Dewhurst.

I am needing help with the understanding, please.

 

 

template <typename T, class A = std::allocator<T>>

class SList{

 

struct Node{

 

};

typedef typename A::template rebind<Node>::other NodeAlloc;

}

 

the following is than written:  "As is typical for lists and other node-based containers, our list-of-T does not actually allocate and manipulate Ts.  Rather, it allocates and manipulates nodes that contain a member of type T."  It continues, "We have some sort of allocator that knows how to allocate objects of type T, but we want to allocate objects of type Node."

 

I am not understanding the allocator, what exactly does it look like when it allocates the node and what is going on with the rebind.  I am unable to understand the inter-workings of the allocator for this SList class.  I am especially confused by the allocator's parameter  list of T?

 

Thank you; sincerely,

Josheir

Edited by Josheir

Share this post


Link to post
Share on other sites
Advertisement
2 hours ago, Josheir said:

I am not understanding the allocator, what exactly does it look like when it allocates the node and what is going on with the rebind. ... I am especially confused by the allocator's parameter list of T?

The allocator allows you to pull memory from a different area than the default memory pools.

By default it uses the general purpose global allocator. Maybe you've got a faster allocator, most games allocate large blocks which they can own in order to avoid the slower, global operating system memory allocation systems that must deal with security systems. Or maybe you've got multiple memory areas on the system (many hardware designs have multiple pools and regions). Or perhaps you have memory that operates on different speeds (most chipsets outside the x86 family have regions with different speeds). 

Whatever the reason, there are functions to get a data block and release a data block.  The allocator's allocate() function gets a bundle of memory, the allocator's deallocate() function releases it.  The class user can use the default memory allocation functions that pull from the OS, or they can provide their own allocator that pulls from their preferred source.

When an item is created, the class will use the allocator to allocate space for the item.  Then it uses the T item's placement new operation to run the constructor at that memory address.  When it is time to be destroyed, the class calls the T item's destructor directly (rather than delete) and then deallocates the memory.

 

The rebind template parameter is a historical oddity that is deprecated in the newest version of the language standard. When a template parameter was also a template that required a parameter, neither the earlier versions of the language nor the tools of the era had the ability to ensure that the template parameters of a template parameter would match.

The 2011 version of the standard had a better solution, and many compilers supported it years before C++11 was complete. 

The rebind member type is deprecated in C++17.  These days it is built in to the classes with an alias template using statement without the need for the rebind member.

Here is a another, probably better explanation. 

 

Share this post


Link to post
Share on other sites

Thank you frob, I'm now understanding.  I still need to look at the alias template link.

Have a Great Holiday Season,

Josheir 

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!