Just learning C++ and trying to understand pointers, new, and delete correctly. About my problem: I create some pointers to a struct and at the end I delete them. But the Heap size does not decrease. I searched for some answers and found the following question.C++ delete does not free all memory (Windows). The code in the answer seems to release the Heap correctly.
My stupid learning code:
#include <iostream>
using namespace std;
struct daten
{
int iVal;
daten *next;
daten *prev;
};
int main()
{
daten *first=nullptr;
daten *prev=nullptr;
daten *entry=nullptr;
daten *last=nullptr;
cout << "[Any Key] FILL";
getchar();
//fill
for( int i=0;i<100000;i++)
{
if(!entry)
{
entry=new daten;
entry->iVal=i;
entry->next=nullptr;
entry->prev=nullptr;
first=entry;
prev=entry;
last=entry;
}
else
{
entry=new daten;
entry->iVal=i;
entry->next=nullptr;
entry->prev=prev;
prev->next=entry;
prev=entry;
last=entry;
}
}
cout << "[Any Key] DELETE";
getchar();
//delete
prev=last;
while(prev)
{
last=prev;
prev=prev->prev;
delete last;
last=nullptr;
}
cout << "[Any Key] END";
getchar();
}
I have a more complex code where I can insert / delete / navigate as a linked list and just to be sure I created 100000 of these entries but after delete the heap does not decrease. So can you please tell me where is my mistake?