Fix a memory bug
[c11tester.git] / stl-model.h
index 489939b739c97878a7a264b4af3d0cc08caf3e3a..cbcae7c04162f979cebb3d338cced3ad5d97e15e 100644 (file)
@@ -59,15 +59,21 @@ public:
        void pop_front() {
                mllnode<_Tp> *tmp = head;
                head = head->next;
-               head->prev = NULL;
+               if (head == NULL)
+                       tail = NULL;
+               else
+                       head->prev = NULL;
                delete tmp;
                _size--;
        }
 
        void pop_back() {
                mllnode<_Tp> *tmp = tail;
-               tail = tail->next;
-               tail->next = NULL;
+               tail = tail->prev;
+               if (tail == NULL)
+                       head = NULL;
+               else
+                       tail->next = NULL;
                delete tmp;
                _size--;
        }
@@ -120,7 +126,7 @@ public:
                if (tail == node) {
                        tail = node->prev;
                } else {
-                       tail->next->prev = node->prev;
+                       node->next->prev = node->prev;
                }
                mllnode<_Tp> *next = node->next;
                delete node;
@@ -211,15 +217,21 @@ public:
        void pop_front() {
                sllnode<_Tp> *tmp = head;
                head = head->next;
-               head->prev = NULL;
+               if (head == NULL)
+                       tail = NULL;
+               else
+                       head->prev = NULL;
                delete tmp;
                _size--;
        }
 
        void pop_back() {
                sllnode<_Tp> *tmp = tail;
-               tail = tail->next;
-               tail->next = NULL;
+               tail = tail->prev;
+               if (tail == NULL)
+                       head = NULL;
+               else
+                       tail->next = NULL;
                delete tmp;
                _size--;
        }
@@ -272,7 +284,7 @@ public:
                if (tail == node) {
                        tail = node->prev;
                } else {
-                       tail->next->prev = node->prev;
+                       node->next->prev = node->prev;
                }
 
                sllnode<_Tp> *next = node->next;
@@ -334,7 +346,7 @@ public:
        }
 
        type back() const {
-               return array[size - 1];
+               return array[_size - 1];
        }
 
        void resize(uint psize) {
@@ -358,11 +370,11 @@ public:
                array[_size++] = item;
        }
 
-       type operator[](uint index) const {
+       type operator[](int index) const {
                return array[index];
        }
 
-       type & operator[](uint index) {
+       type & operator[](int index) {
                return array[index];
        }
 
@@ -463,11 +475,11 @@ public:
                array[_size++] = item;
        }
 
-       type & operator[](uint index) {
+       type operator[](int index) const {
                return array[index];
        }
 
-       type operator[](uint index) const {
+       type & operator[](int index) {
                return array[index];
        }