Hello, I have a question why the memory after the delete operator, the memory remains involved, or something I do not understand:

#include <iostream> using namespace std; int main(){ int *p=new int[10]; for(int i=0;i<10;i++){ p[i]=i; } delete [] p;//удаление массива cout<<p[2];//и после удаления почему то благополучно выводится значение } 

    4 answers 4

    About how to remove arrays, you have already written. I will write about why access to old data is preserved.

    Even if you correctly remove dynamically allocated memory, the connected memory pages are not always unloaded from the heap, especially if they are in the middle of the heap. Simply, they are marked as free and can be used if there is enough space when they re-request a memory allocation. This speeds up the process of dynamic memory allocation, which is quite resource-intensive.

    • Clearly, I thought that this memory was no longer in use, thanks for the reply. - username76
    • In fact, it can be unloaded if there is a lot of it (more than the size of the page) and it is located at the end of the heap. It all depends on the standard library. - skegg

    First of all, you are not correctly performing the removal of the array - you must delete it

     delete [] p; 

    Secondly, after the release of memory, the contents are usually not cleared, just the area is marked as free. Therefore, old values ​​may remain there for some time.

    • A typo came out, I meant delete [] p; and how do I remove permanently? - username76
    • First, zero all memory locations, for example, using the memset function, and then unload by delete. But why? - skegg pm
    • Do you keep something secret there? You can score before removing zeros. - dzhioev pm
    • 2
      Also recommend after delete [] p; Use p = 0; Then repeated delete (for forgetfulness) does not promise trouble. - BuilderC
    • one
      @skegg, the compiler has the right to remove the memset call if the memory is not in use. And he will do that. You can not erase secret data using memset. - Qwertiy

    What is created with new [] should be deleted with delete []. Otherwise there will be leaks. And the fact that the value is output is the compiler did not inspect and did not give on the fingers, but in general it would be used to generate exceptions to memory access (although it is not necessary in release versions).

    That is, the fact that you turned to the memory that was released and that something was derived there is just UB (undefined behavior). In the example above, nothing bad is likely to happen, but in some kind of loaded system, a completely different meaning may appear (at best).

    • Typo, corrected. - username76
    • .... in the worst case - the end of the world, if the system is designed for a worldwide network of nuclear power plants =) - AseN
    • @KoVadim, and where did you see the exception to memory access (by simple SIGSEGV) when accessing memory freed by delete (or free)? Name the system, please. - avp
    • one
      Nobody spoke about SIGSEGV. By the word "exception" I understood a more general version. No one bothers to use various specialized memory managers to track calls to freed memory. In VisualStudio C ++, the compiler can fill the memory with special values ​​to track such hits. - KoVadim
    • > What is created with new [] should be deleted with delete []. Otherwise there will be leaks. I saw in Boreskov’s book "OpenGL Extensions" such an error when he allocated memory for an image through new [], and deleted it via delete. Moreover, not somewhere once, but everywhere. - gammaker

    After the completion of the program, all the occupied memory will still be freed, so that you can not worry about it. In general, be careful here, in your code, due to ridiculous carelessness, a memory leak may occur.

    • one
      > After the completion of the program, all the occupied memory will still be freed, so that you can not worry about it. This is considered a bad programming tone. Then you change something, the program will grow, and you will forget to delete. - gammaker
    • Well, this is in linear, simple algorithms such as splitting lines, etc. In complex systems that use patterns, of course, these need to be monitored. - AseN