Reputation:
I've tried several ways to fix this, but cant seem to figure it out. Valgrind is pointing to a memory leak with my resize method and I feel like I might be missing something simple.
.h file
private:
int* pArray; // stores math expression
int currentSize; // size of array
.cpp
void Prog::resize(int newSize)
{
cout << "Address of polynomial:\t\t\t" << &pArray << endl;
int* temp = new int[newSize] {0};
cout << "Address of temp:\t\t\t" << &temp << endl;
copy(temp, pArray);
delete[] pArray;
pArray = temp;
cout << "Address of pArray after swap:\t" << &pArray << endl;
temp = nullptr;
currentSize = newSize;
}
void Prog::copy(int* to, int* from)
{
for (int i = 0; i < currentSize; i++)
to[i] = from[i];
}
I added the cout's to see what is happening with the address because I thought after swapping the pArray to temp that it would print out the address of what temp's location, but it seems to keep its original location. Is that what is supposed to happen?
I've tried creating a swap method and the issue is still the same when I use it.
void Prog::swap(int*& to, int*& from)
{
int* temp = to;
to = from;
from = temp;
}
This is the output when I run my program and from Valgrind.
Program snip
Address of pArray: 0000006741EFF218
Address of temp: 0000006741EFEEE8
Address of pArray after swap: 0000006741EFF218
D = +50x^20000 +15x^11 +5x^10 -12x^7 -4x^6 +30x^5 +4x^4 -2x^3 +50
Valgrind
==22696== 24 bytes in 1 blocks are definitely lost in loss record 1 of 12
==22696== at 0x4C2E80F: operator new[](unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==22696== by 0x40230A: Prog::resize(int) (Prog.cpp:332)
==22696== by 0x401DD7: Prog::operator=(Prog const&) (Prog.cpp:171)
==22696== by 0x400DEE: main (lab1.cpp:36)
==22696==
==22696== 36 bytes in 1 blocks are definitely lost in loss record 2 of 12
==22696== at 0x4C2E80F: operator new[](unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==22696== by 0x40230A: Prog::resize(int) (Prog.cpp:332)
==22696== by 0x401DD7: Prog::operator=(Prog const&) (Prog.cpp:171)
==22696== by 0x400DD5: main (lab1.cpp:35)
==22696==
==22696== 52 bytes in 1 blocks are definitely lost in loss record 9 of 12
==22696== at 0x4C2E80F: operator new[](unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==22696== by 0x40230A: Prog::resize(int) (Prog.cpp:332)
==22696== by 0x401DD7: Prog::operator=(Prog const&) (Prog.cpp:171)
==22696== by 0x40201D: Prog::operator*=(Prog const&) (Prog.cpp:214)
==22696== by 0x40116D: main (lab1.cpp:55)
Any help is appreciated!
Edit - The assignment operator shows a memory leak as well but it uses the resize method which is why I left it out, but here rest of the code requested:
Prog::Prog() : currentSize(1)
{
pArray = new int[currentSize] {0};
}
Prog::~Prog()
{
for (int i = 0; i < currentSize; i++)
this->pArray[i] = 0;
currentSize = 0;
delete[] pArray;
pArray = nullptr;
}
Prog& Prog::operator=(const Prog& rhs)
{
if (this == &rhs)
return *this;
for (int i = 0; i < currentSize; i++)
pArray[i] = 0;
if (this->currentSize < rhs.currentSize)
{
resize(rhs.currentSize + 1);
currentSize = rhs.currentSize;
pArray = new int[currentSize];
for (int i = 0; i < currentSize; i++)
pArray[i] = rhs.pArray[i];
}
else
{
for (int j = 0; j < rhs.currentSize; j++)
pArray[j] = rhs.pArray[j];
}
return *this;
}
Upvotes: 1
Views: 344
Reputation: 32727
In your operator=
, you call resize
, then essentially do the same thing again in the next two statements. Since resize
allocates memory (and stores that pointer into pArray
, then you overwrite that value in operator=
with a new one without freeing the previous value, you get the leak.
Upvotes: 2