Thread: Keeping track of STL container memory usage

  1. #1
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477

    Keeping track of STL container memory usage

    The topic says most of it.

    I'm having fun with various algorithms and their performance, and I've written a handler class that runs the various algorithms and displays statistics afterwards.

    I remember a thread about insertion sort by RichSelian where he displayed statistics from various sorting algorithms - I'm trying to do almost exactly the same thing, except I'm not testing sorting algorithms.
    All my algorithm functor classes derive from a base class which has some pure virtual functions the specific derivatives have to define, and then it defines a wrapper around new, which the algorithm functors will be calling every time they do a manual allocation. But this solution doesn't work with STL containers, and one of the main reasons I'm doing this is to measure performance between the dynamically resizable STL containers and arrays.


    Will I have to write a custom allocator for the STL containers or something in that direction?

    I'd really appreciate any information on this subject - links, tips, "no, that's stupid", etc.

    Thanks for reading.
    "What's up, Doc?"
    "'Up' is a relative concept. It has no intrinsic value."

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I don't know if .capacity() would suit your needs, since you probably don't want to check it all the time. But that was the whole point of the custom allocator in the STL, so far as I know, was to give a way to use custom-new and custom-delete.

    I've never dealt with custom allocators, so I can't help much there. If you've got something written, it might not be that much more work to make it fit the std allocator criteria (I can probably quote what the standard says needs to be defined, if necessary).

  3. #3
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    Custom allocator is good
    Code:
    #include <vector>
    #include <iostream>
    
    template<class T>
    class talloc: public std::allocator<T>
    {
    
    public:
          pointer allocate( size_type _Count, const void* _Hint)
          {
                //return new(_Count* sizeof(T));
                return std::allocator<T>::allocate( size_type _Count, const void* _Hint);
          }
          void deallocate(pointer _Ptr, size_type _Count)
          {
                //for(pointer p =0; p <_Count; ++p)
                //	delete (_Ptr+p);
                std::allocator<T>::deallocate(pointer _Ptr, size_type _Count);
          }
    };
    
    int main()
    {
          std::vector<int, talloc<int>> tv(10);
          tv.push_back(23);
          std::cout << tv.back();
          return 0;
    }
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. auto_ptr and const atributes in stl container
    By yezaim in forum C++ Programming
    Replies: 4
    Last Post: 10-09-2009, 03:33 PM
  2. Keep track of elements inside STL vector
    By Krones in forum C++ Programming
    Replies: 7
    Last Post: 09-12-2009, 12:39 PM
  3. Storing 5 Game Scores- Which STL Container?
    By bengreenwood in forum C++ Programming
    Replies: 15
    Last Post: 08-23-2009, 09:43 PM
  4. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM
  5. Is it necessary to write a specific memory manager ?
    By Morglum in forum Game Programming
    Replies: 18
    Last Post: 07-01-2002, 01:41 PM