smart pointer+allocator?

This is a discussion on smart pointer+allocator? within the C++ Programming forums, part of the General Programming Boards category; Hi everyone, Instead of smart pointers is it not usually better to use something like: Code: Object obj = pool.get(memory_size); ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    40

    smart pointer+allocator?

    Hi everyone,

    Instead of smart pointers is it not usually better to use something like:
    Code:
    Object obj = pool.get(memory_size);
    //std::cout.write(static_cast<const char*>(obj.front()),memory_size);
    And then get at least [memory_size] free aligned memory? Object class will then work exactly as a smart pointer, but instead of deleting the memory it just push it back to the pool and therefore can it be reused again. A summarize is smart pointers together with allocators.

    It sounds of course that it should be more efficient, but if it should, why are it used so rarely?

    Thanks in advance.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    The decision to use a special allocator (like a pool allocator) is separate from the decision to use a smart pointer. They solve different problems. You can use one or the other or both or neither with no problem.

    The smart pointer governs ownership of a resource and helps determine when the memory it uses can be freed to be used again. The pool allocator is just used to determine where to get the memory from and helps reduce the number of actual calls to allocate and deallocate. If you use a pool allocator, you still need code to determine when to free the memory back into the pool, and that's what a smart pointer can help you with.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    40
    Well, I know that, but I have never seen anyone use smart pointers together with allocators of any kind, even if I am not I a programming freak who have it neither as a job or doing it many hours per day. I have read pretty much lines of code in my life comparing to my age but never have them been used together. Can it be so that it is maybe much better to use a optimized malloc function as goggle's tcmalloc instead of smart pointers+allocators, less overhead (the allocator most support thread safety)? But of course, it should probably much less error prone and better to just overload global new/delete and pass it to the memorypool then, why have I not thought about if before now

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    I think you may be right. If you're going to use the special allocator, in most cases you might as well do it for your entire program (or module) rather than do it just for some smart pointers.

    I have had to use a pool allocator just for certain containers, so even though they aren't exactly smart pointers there are definitely instances where you might want to use an allocator only for a specific class or classes.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Which smart pointer to use?
    By leeor_net in forum C++ Programming
    Replies: 4
    Last Post: 04-13-2009, 04:29 AM
  2. Why does C need pointer conversion
    By password636 in forum C Programming
    Replies: 2
    Last Post: 04-10-2009, 07:33 AM
  3. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM

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