Not logged in, Join Here! or Log In Below:  
 
News Articles Search    
 

 Home / General Programming / minimalist stl vector alike template Account Manager
 
Archive Notice: This thread is old and no longer active. It is here for reference purposes. This thread was created on an older version of the flipcode forums, before the site closed in 2005. Please keep that in mind as you view this thread, as many of the topics and opinions may be outdated.
 
sylphaleya

May 18, 2005, 04:19 AM

Whenever I use my own memory manager together with G++ it looks like the stl has some memory leaks. When I use ICC (which has a different stl) there aren't any obvious leaks. However I've tried to do my own minimalistic vector implementation, because this is the only class I need anyway (therefore I can watch memory allocation as I
like).

So far the class works as I expect, but maybe there are some errors I'm not aware off.

Thanks in advance for any corrections, bug fixes, ....

  1.  
  2. class Memory {
  3. public:
  4.         static void* take(const unsigned int iSize) {
  5.                 void* pAddress = ::malloc(iSize);
  6.                 return pAddress;
  7.         }
  8.  
  9.         static void* resize(void* pAddress, const unsigned int iSize) {
  10.                 return ::realloc(pAddress, iSize);
  11.         }
  12.  
  13.         static void free(void* pAddress) {
  14.                 ::free(pAddress);
  15.         }
  16. };
  17.  
  18. void* operator new(unsigned int iSize, void* pAddress) {
  19.         return pAddress;
  20. }
  21.  
  22. template <typename T> class List {
  23. private:
  24.         unsigned int miLength;
  25.         unsigned int miCapacity;
  26.         unsigned int miIncrement;
  27.         T* mtRaw;
  28.  
  29. public:
  30.         explicit List(const unsigned int iCapacity, const unsigned int iIncrement = 1)
  31.         :
  32.         miLength(0),
  33.         miCapacity(iCapacity),
  34.         miIncrement(iIncrement),
  35.         mtRaw(static_cast<T*>(Memory::take(iCapacity * sizeof(T)))) {
  36.                 //ASSERT(miIncrement >= 1)
  37.         }
  38.  
  39.         List(const List<T>& lst)
  40.         :
  41.         miLength(lst.miLength),
  42.         miCapacity(lst.miCapacity),
  43.         miIncrement(lst.miIncrement),
  44.         mtRaw(static_cast<T*>(Memory::take(lst.miCapacity * sizeof(T)))) {
  45.                 for(unsigned int i = 0; i < lst.miLength; i++) {
  46.                         new (mtRaw + i) T(lst.mtRaw[i]);
  47.                 }
  48.         }
  49.  
  50.         List<T>& operator= (const List<T>& lst) {
  51.                 if (this == &lst) {
  52.                         return *this;
  53.                 }
  54.  
  55.                 T* tRaw = static_cast<T*>(Memory::take(lst.miCapacity * sizeof(T)));
  56.                 for(unsigned int i = 0; i < lst.miLength; i++) {
  57.                         new (tRaw + i) T(lst.mtRaw[i]);
  58.                 }
  59.  
  60.                 clear();
  61.                 Memory::free(mtRaw);
  62.  
  63.                 miLength = lst.miLength;
  64.                 miCapacity = lst.miCapacity;
  65.                 miIncrement = lst.miIncrement;
  66.                 mtRaw = tRaw;
  67.  
  68.                 return *this;
  69.         }
  70.  
  71.         ~List() {
  72.                 clear();
  73.                 Memory::free(mtRaw);
  74.         }
  75.  
  76.         void clear() {
  77.                 for(unsigned int i = 0; i < miLength; i++) {
  78.                         mtRaw[i].~T();
  79.                 }
  80.                 miLength = 0;
  81.         }
  82.  
  83.         void append(const T& t) {
  84.                 if (miLength >= miCapacity) {
  85.                         miCapacity += miIncrement;
  86.                         mtRaw = static_cast<T*>(Memory::resize(mtRaw, miCapacity * sizeof(T)));
  87.                 }
  88.                 new (mtRaw + miLength) T(t);
  89.                 miLength++;
  90.         }
  91.  
  92.         unsigned int capacity() const {
  93.                 return miCapacity;
  94.         }
  95.  
  96.         unsigned int length() const {
  97.                 return miLength;
  98.         }
  99.  
  100.         T& operator[] (unsigned int iPosition) const {
  101.                 //ASSERT(iPosition < miLength)
  102.                 return mtRaw[iPosition];
  103.         }
  104.  
  105.         const T* raw() const {
  106.                 return mtRaw;
  107.         }
  108. };
  109.  
  110.  


Please don't blame me that I should use stl. I've not written that the (G++) stl has a bug.

 
juhnu

May 18, 2005, 05:13 AM

If this meant to be a replacement for the std::vector I guess you should at least implement the most common feature set of it. From a very quick glance I can say that you are at least missing the iterators, lots of methods are missing and signatures are wrong, your constructor is behaving fundamentally different way than the std::vector's etc..

You could use some open STL implementation instead, if you have problems with the one G++ has.



juhani


 
sylphaleya

May 18, 2005, 05:57 AM

It was never meant as replacement (then I could use stl). The only similarity is the behavour that all elements share a contiguous region.
IMO are ctor, copy-ctor, ... the most problematic methods. Everything else is sugar, and only simplfies access.

 
Fabian 'ryg' Giesen

May 18, 2005, 09:34 AM

Whenever I use my own memory manager together with G++ it looks like the stl has some memory leaks.


You should first check where you do your leak checking. If the g++ standard C library uses pooling allocators or similar schemes, it's likely that they only get freed during C/C++ library shutdown, which is after you return from main() (or WinMain() or whatever).

 
juhnu

May 18, 2005, 08:05 PM

"Please don't blame me that I should use stl. I've not written that the (G++) stl has a bug."

I got an impression from this you wanting to make your own replacement due to the STL memory problems you had.


juhani


 
This thread contains 5 messages.
 
 
Hosting by Solid Eight Studios, maker of PhotoTangler Collage Maker.