allocator implementation

This is a discussion on allocator implementation within the C++ Programming forums, part of the General Programming Boards category; Hello everyone, I have debugged into STL implementation for allocator in file xmemory (Visual Studio 2008). I found it has ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    allocator implementation

    Hello everyone,


    I have debugged into STL implementation for allocator in file xmemory (Visual Studio 2008). I found it has not data members, the same as its base class _Allocator_base. Is that correct?

    http://msdn2.microsoft.com/en-us/library/6s8wyf7c.aspx

    My questions is what is the purpose of copy? It always returns true. Here is the code,

    you can see do nothing for copy constructor as well.

    Code:
    allocator(const allocator<_Ty>&) _THROW0()
    {
        // construct by copying (do nothing)
    }
    
    		// allocator TEMPLATE OPERATORS
    template<class _Ty,
    	class _Other> inline
    	bool operator==(const allocator<_Ty>&, const allocator<_Other>&) _THROW0()
    	{	// test for allocator equality (always true)
    	return (true);
    	}

    thanks in advance,
    George

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I found it has not data members, the same as its base class _Allocator_base. Is that correct?
    Yes.

    >My questions is what is the purpose of copy?
    What copy? There's no copy member function in the allocator class. Do you mean the assignment operator? That and the copy constructor are for the allocator objects, and because the default allocator doesn't maintain any internal state (it just uses new/delete), they don't need to do anything beyond the default behavior.

    Why do you think this is wrong?
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Sorry, Prelude. I do not think it is wrong. You reply is helpful and I agree. :-)


    My question is, for example, the operator==, it always returns true, is it useful?

    Quote Originally Posted by Prelude View Post
    >I found it has not data members, the same as its base class _Allocator_base. Is that correct?
    Yes.

    >My questions is what is the purpose of copy?
    What copy? There's no copy member function in the allocator class. Do you mean the assignment operator? That and the copy constructor are for the allocator objects, and because the default allocator doesn't maintain any internal state (it just uses new/delete), they don't need to do anything beyond the default behavior.

    Why do you think this is wrong?

    regards,
    George

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,262
    Quote Originally Posted by George2 View Post
    My question is, for example, the operator==, it always returns true, is it useful?
    Since all standard allocators work the same way, it makes sense they compare equal.

    If you create your own custom allocator class then you will (probably) not want it to test as equal to a standard allocator. So you would supply versions of operator==() that behave accordingly.

    You would create your own custom allocator class if you needed to manage dynamic memory using a different means than operators new/delete. For example, if you needed to allocate memory in some memory pool that is shared between processes.

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks grumpy,


    Question answered.

    Quote Originally Posted by grumpy View Post
    Since all standard allocators work the same way, it makes sense they compare equal.

    If you create your own custom allocator class then you will (probably) not want it to test as equal to a standard allocator. So you would supply versions of operator==() that behave accordingly.

    You would create your own custom allocator class if you needed to manage dynamic memory using a different means than operators new/delete. For example, if you needed to allocate memory in some memory pool that is shared between processes.

    regards,
    George

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21