Hi,
I want to know what is allocator in C++ STL exactly ?
For example what is the difference between the two following statements :

vector< int, allocator< int > > v1;
vector< int, allocator< char > > v2;

Recommended Answers

All 4 Replies

>I want to know what is allocator in C++ STL exactly ?
An allocator doles out blocks of memory using a standardized interface so that we can switch the allocation method quickly and easily. For example, we can go from using the standard allocator to using a memory pool simply by plugging in a different allocator class. The std::allocator class is the default allocator which uses new and delete .

>For example what is the difference between the two following statements
The first is correct, the second is broken. The first line is actually redundant. That's what happens by default anyway. The second line is kind of stupid because you're specifying an allocator for a different size type than the items stored in the vector.

Thanks.........
So if the first line is actually redundant and the second is broken, we don't need to specify it . Right ?
We can use this easily :
vector< int > v3;

the first line is actually redundant , then what is the allocator for?

Because you can write

vector<int, some_other_allocator<int> > v;

and if some_other_allocator is appropriately defined, you can impose a vector data structure on memory that is allocated in a way that some_other_allocator defines.

Many people will go through their entire careers without ever needing to do this, but it's there for the applications that need it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.