-3

I am very confused about this simple code. why this code can release the memory allocated.

double *a;
for(int i = 0 ; i < 1000000 ; i++)
{
    if(1){
        a = new double ;
    }
    if(1){
        delete a;
    }
}

but this code can not remove all memory allocated.

std::vector<double *>rubberList ;
for(int i = 0 ; i < 1000000 ; i++)
{
    rubberList.push_back(new double);
    //delete rubberList[i];
}
for(unsigned long j = 0 ; j < 1000000 ; j++)
{
            delete rubberList.at(j);
}

and when delete items in the allocator block correctly remove the memory.

for(int i = 0 ; i < 1000000 ; i++)
{
    rubberList.push_back(new double);
    delete rubberList[i];
}

tnx

SajadBlog
  • 514
  • 2
  • 12
  • 3
    *Why* exactly do you believe the middle code segment isn't freeing the memory? If it's because some external monitor says the process takes up space but doesn't release it, see https://stackoverflow.com/questions/54628096/c-deleting-object-do-not-free-memory/54628160#54628160 – paxdiablo Feb 16 '19 at 06:01
  • 3
    The first (and third) version repeatedly allocate _one_ `double` and release it. So, chances are best that it's always the same address that you get. So, the overall requirement concerning this is 1 `double` in heap. The second version instead allocates all 1000000 before the first is released. Your appl. (the heap management in std library) will request the appropriate memory from OS but it might not give it back as soon as you release it. Hence, you see the much bigger footprint in OS stats. – Scheff's Cat Feb 16 '19 at 06:02
  • try changing your first code to allocate all million pointers before freeing any – M.M Feb 16 '19 at 06:17
  • tnx about comments. so when i want monitor memory allocation and see rapidly after release memory must wait for exit application ? i have a big class and want check all memory that allocated released.but according to answers there is no way to check this routine – SajadBlog Feb 16 '19 at 06:20
  • Your code segments behave differently. The first continually allocates and immediately deallocates a single `double.` The second allocates `1000000` doubles (one at a time) pushes them into a vector, which allocates MORE memory to hold all those `1000000` pointers, and then only releases the `1000000` doubles without cleaning up the memory allocated by the vector. The third is similar to the second, in that it causes a `vector` to grow without ever deallocating - the only difference is that the individual `doubles` are all deallocated immediately after being added to the vector. – Peter Feb 16 '19 at 06:20
  • 1
    @SajadHosseini *so when i want monitor memory allocation and see rapidly after release memory must wait for exit application* -- If you need to be convinced that the external tools do not tell you the "truth", put your code in a function that allocates a lot of memory, and on exit deallocates the memory. Then call this function 1000 times. You will see that even though your external tool shows memory not being deallocated, the memory usage doesn't rise, even though you called the function 1000 times. How do you explain that? – PaulMcKenzie Feb 16 '19 at 06:27
  • @PaulMcKenzie ok ;) – SajadBlog Feb 16 '19 at 06:32

1 Answers1

0

The middle example that you posted does release all the memory. For even more proof, I compiled your code and ran it with valgrind:

==5829== Memcheck, a memory error detector

==5829== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.

==5829== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info

==5829== Command: test

==5829==

==5829==

==5829== HEAP SUMMARY:

==5829== in use at exit: 0 bytes in 0 blocks

==5829== total heap usage: 30 allocs, 30 frees, 4,065 bytes allocated

==5829==

==5829== All heap blocks were freed -- no leaks are possible

==5829==

==5829== For counts of detected and suppressed errors, rerun with: -v

==5829== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

The code you posted does clean up the memory. The only thing it does differently is probably use more memory since you allocate a ton of doubles in the middle one but only one at a time in the first one.

Where is your evidence that it does not remove the memory? Or do you mean something else?

Community
  • 1
  • 1
Water
  • 3,245
  • 3
  • 28
  • 58