Question: can someone help me debug the leaker errors on thios code ABS.H #pragma once #include #include #include using namespace std; template class ABS { private:
can someone help me debug the leaker errors on thios code ABS.H
| #pragma once | |
| #include | |
| #include | |
| #include | |
| using namespace std; | |
| template | |
| class ABS { | |
| private: | |
| unsigned int capacity; | |
| unsigned int count; | |
| float scalerFactor = 2.0f; | |
| T* array; | |
| public: | |
| ABS(); | |
| ABS(int capacity); | |
| ABS(const ABS &d); | |
| ABS&operator = (const ABS &d); | |
| ~ABS(); | |
| void push(T &data); | |
| T pop(); | |
| T peek(); | |
| unsigned int getSize(); | |
| unsigned int getMaxCapacity() const; | |
| T* getData(); | |
| const T &operator[] (unsigned int index) const; | |
| T &operator[] (unsigned int index); | |
| }; | |
| template | |
| inline ABS | |
| this->capacity = 1; | |
| this->count = 0; | |
| array = new T[capacity]; | |
| } | |
| template | |
| inline ABS | |
| { | |
| this->capacity = capacity; | |
| this->count = 0; | |
| array = new T[capacity]; | |
| } | |
| template | |
| inline ABS | |
| { | |
| this->count = d.count; | |
| this->capacity = d.capacity; | |
| this->array = new T[capacity]; | |
| for (unsigned int i = 0; i < capacity; i++) { | |
| array[i] = d[i]; | |
| } | |
| } | |
| template | |
| ABS | |
| if (this == &d) { | |
| return *this; | |
| } | |
| if (array != nullptr) { | |
| delete[] array; | |
| } | |
| this->count = d.count; | |
| this->capacity = d.capacity; | |
| this->array = new T[capacity]; | |
| for (unsigned int i = 0; i < capacity; i++) { | |
| array[i] = d[i]; | |
| } | |
| return this->*array; | |
| } | |
| template | |
| inline ABS | |
| if (array != nullptr) { | |
| delete[] array; | |
| } | |
| } | |
| template | |
| void ABS | |
| if (count == capacity) { | |
| this->capacity *= scalerFactor; | |
| } | |
| this->array[count] = data; | |
| //cout << "array at count: " << array[count] << endl; | |
| count++; | |
| } | |
| template | |
| inline T ABS | |
| if (count == 0) { | |
| //throw runtime_error("Error!"); | |
| return -1; | |
| } | |
| //cout << "count/cap: " << ((float)count / (float)capacity) << endl; | |
| if (count < 0.5*(float)capacity) { | |
| capacity = capacity / scalerFactor; | |
| } | |
| T returnValue = array[count-1]; | |
| T* tempArray = new T[capacity]; | |
| for (unsigned int i = 0; i < count - 1; i++) { | |
| //cout << "Int copied over: " << array[i] << endl; | |
| tempArray[i] = array[i]; | |
| } | |
| for (unsigned int i = count; i < capacity+1; i++) { | |
| tempArray[i] = 0; | |
| } | |
| if (array != nullptr) { | |
| delete[] array; | |
| } | |
| array = tempArray; | |
| count--; | |
| return returnValue; | |
| /*if (count == 0) { | |
| return -1; | |
| } | |
| if (count == capacity) { | |
| capacity--; | |
| } | |
| T* tempArray = new T[capacity]; | |
| for (unsigned int i = 0; i < count; i++) { | |
| this->array[i] = this->array[1]; | |
| } | |
| count--; | |
| if (array != nullptr) { | |
| delete[] array; | |
| } | |
| this->array = tempArray; | |
| return tempArray[count+1];*/ | |
| } | |
| template | |
| inline T ABS | |
| { | |
| if (capacity == 0) { | |
| return -1; | |
| } | |
| else { | |
| //cout << "element at the top of the stack" << getData()[count-1] << endl; | |
| return array[count-1]; | |
| } | |
| } | |
| template | |
| inline unsigned int ABS | |
| { | |
| return count; | |
| } | |
| template | |
| inline unsigned int ABS | |
| { | |
| return capacity; | |
| } | |
| template | |
| inline T * ABS | |
| { | |
| return array; | |
| } | |
| template | |
| inline const T & ABS | |
| { | |
| if (index > capacity || index < 0) { | |
| //throw runtime_error("Error! Invalid index"); | |
| } else { | |
| return array[index]; | |
| } | |
| } | |
| template | |
| inline T & ABS | |
| { | |
| if (index > capacity || index < 0) { | |
| //throw runtime_error("Error! Invalid index"); | |
| } | |
| else { | |
| return array[index]; | |
| } | |
| } | |
I am getting the below errors
LEAKER: unknown:unknown():0 checking error: wrote off end of memory allocated at unknown:unknown():0. *** Error in `./a.out': free(): invalid next size (fast): 0x000000000092ec40 *** ======= Backtrace: ========= /lib/x86_64-linux-gnu/libc.so.6(+0x777e5)[0x7fd919e287e5] /lib/x86_64-linux-gnu/libc.so.6(+0x8037a)[0x7fd919e3137a] /lib/x86_64-linux-gnu/libc.so.6(cfree+0x4c)[0x7fd919e3553c] ./a.out(_ZdaPv+0x64)[0x402fc0] ./a.out[0x401e7b] ./a.out[0x40124b] ./a.out[0x40107b] /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf0)[0x7fd919dd1830] ./a.out[0x400f79]
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
