1

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?

4
  • 3
    how are you monitoring the size of the heap? Commented Nov 22, 2013 at 1:42
  • Using Linux and KDE Systemmonitor can display the Heap size. Commented Nov 22, 2013 at 1:44
  • it is up to glibc to shrink the size of the heap whenever it feels like it. a lot of times the virtual memory size is like a high water mark, it just indicates the maximum amount of memory your program ever used at one time. i don't think there is any mistake in your program, just an ism of glibc. Commented Nov 22, 2013 at 1:51
  • Thats a high mark was my think also, in my other code after adding some data heap went to XX , delete and add twice as much heap went to 2XX and after delete and create less then 2XX heap did not encrease so it looked like the highest amount used. But I was not sure if it my structs got really deleted. But in the linked example the Heap goes to 4GB (cant remember) and gets completely released. Thats why im worried. Just want to understand and learn correctely. Commented Nov 22, 2013 at 2:00

1 Answer 1

1

If you want to understand the heap usage of your program, with respect to being sure you are using malloc/free and new/delete correctly, then you should be using an application-level tool like Valgrind Memcheck.

The system level tools will generally report the amount of virtual memory that has been reserved for the heap, which is not all necessarily committed. The virtual memory reserved for the heap will increase during an application's lifetime, only periodically being reduced at the whim of the runtime when it determines to compact it.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. Looks like a real good helper :-) With my code I got: "total heap usage: 100,000 allocs, 100,000 frees, 2,400,000 bytes allocated" and "All heap blocks were freed -- no leaks are possible" So it looks good

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.