I started learning c ++ and I ran into this problem. If I do not use the destructor, then everything works fine, but when I add it, the following error occurs:
*** Error in `./arraylist.o': double free or corruption (fasttop): 0xb71ab360 ***
./test_arraylist.sh: line 2: 513 Aborted ./arraylist.o
My source code:
#include <iostream>
#include "jppsys.h"
namespace jpparl {
template <class T> class ArrayList {
private:
T *array;
int step;
long totalSize;
long usedSize = 0;
public:
ArrayList(long step) {
this->step = step;
totalSize = step;
array = new T[step];
std::cout << "\nCreate " << array << "\n";
}
ArrayList() {
this->step = 8;
totalSize = step;
array = new T[step];
std::cout << "\nCreate " << array << "\n";
}
ArrayList(const ArrayList &arraylist) {
step = arraylist.step;
totalSize = arraylist.totalSize;
usedSize = arraylist.usedSize;
array = new T[totalSize];
std::copy(arraylist.array, arraylist.array + totalSize, array);
std::cout << "\nCopy " << array << "\n";
}
~ArrayList() {
std::cout << "\nDelete " << array << "\n";
// delete[] array;
// ^^^^^^^^^^^^^^^ error here
}
void add(T elem, long index) {
if (usedSize == totalSize) totalSize += step;
T *tmp = new T[totalSize];
std::copy(array, array + index, tmp);
std::copy(array + index, array + index + usedSize, tmp + index + 1);
delete[] array;
tmp[index] = elem;
array = tmp;
usedSize++;
}
void remove(long index) {
if (usedSize == totalSize - step) totalSize -= step;
T *tmp = new T[totalSize];
std::copy(array, array + index, tmp);
std::copy(array + index + 1, array + usedSize, tmp + index);
delete[] array;
array = tmp;
usedSize--;
}
T get(long index) {
return array[index];
}
long size() {
return usedSize;
}
long getTotalSize() {
return totalSize;
}
};
}
using namespace jpparl;
using namespace std;
int main() {
ArrayList<ArrayList<int>> al(1);
ArrayList<int> al2(1);
cout << "\nAdding 256\n";
al2.add(256,0); // al2.get(0) - 256
cout << "\nAdding al2\n";
al.add(al2, 0); // adds copy of a2, al.get(0) - copy of.a2
cout << "\nRemoving 256\n";
al2.remove(0); // removes 256 from al2, not from a1
cout << al2.get(0) << " - must be 0 " << al.get(0).get(0) << " - must be 256\n";
cout << al2.size() << " - must be 0 " << al.get(0).size() << " - must be 1\n";
}
And program outputs:
Without destructor:
Create 0xb86d4f30
Create 0xb86d4f18
Create 0xb86d5360
Adding 256
Adding al2
Copy 0xb86d5360
Create 0xb86d53a0
Delete 0xb86d4f30
Delete 0xb86d5360
Removing 256
0 - must be 0
Copy 0xb86d5370
256 - must be 256
Delete 0xb86d5370
0 - must be 0
Copy 0xb86d53d8
1 - must be 1
Delete 0xb86d53d8
Delete 0xb86d53c8
Delete 0xb86d5388
With destructor:
Create 0xb71aaf30
Create 0xb71aaf18
Create 0xb71ab360
Adding 256
Adding al2
Copy 0xb71ab360
Create 0xb71ab3a0
Delete 0xb71aaf30
Delete 0xb71ab360
Removing 256
0 - must be 0
Copy 0xb71ab370
0 - must be 256
Delete 0xb71ab370
0 - must be 0
Copy 0xb71ab370
1 - must be 1
Delete 0xb71ab370
Delete 0xb71ab360
Delete 0xb71ab388
Delete 0xb71ab360
*** Error in `./arraylist.o': double free or corruption (fasttop): 0xb71ab360 ***
./test_arraylist.sh: line 2: 513 Aborted ./arraylist.o
I will be grateful for help