+ 1
[Solved] What will happen if we don't delete an array created by using Dynamic Memory in C++?
From what I read in the comments in the lesson, an array created using pointers and dynamic memory store in the Heap (in C# concept), while a normal array store in Stack. C# has a garbage collector to free the Heap. What about C++? The lesson taught us about "delete [] 'arr_name'" to free the memory. And that is it, without saying the consequence if we don't delete it. In the old days when memory is expensive (say 640kb), it is easy to imagine running out of memory if we keep creating dynamic array without deleting the old arrays. But what will happen if we don't delete it AND close the application? Will the memory is freed once the application closed, or those arrays stay in memory but no one can use it?
3 Answers
+ 2
While the app is running, you will have memory leak as the memory allocated for that array remain reserved. After you close the app, on the OS level generally it shouldn't be a problem as the most mainstream operating systems (windows, Linux, mac os etc.) keep track of memory allocations that each application uses and reclaim it upon app termination (given that it didn't crash). Problem might be when you run such app on a custom os (such as buildroot linux) or embedded system as some of them use different memory managers. Anyway you shouldn't rely on OS memory managers because when your app grow and become more complex, it will potentially use several different (seemingly unrelated) processes, so try to keep track of potential memory leaks wherever you can.
+ 3
it shouldn't be a problem as the most mainstream operating systems (windows, Linux, mac os etc.) keep track of memory allocations that each application uses and reclaim it upon app termination (given that it didn't crash).
>> Does it mean if an application crash, those allocated memory is unrecoverable until a system reboot?
try to keep track of potential memory leaks wherever you can.
>> How to archive this goal? Add the "delete [] 'arr_name'" with every "new" array?
+ 2
âDoes it mean if an application crash, those allocated memory is unrecoverable until a system reboot?â
>> potentially yes if the OS wonât reclaim it properly. Note that each instance of an app have itâs own memory allocation created by os - that includes stack and heap. If your app crash without any warning or error message (simply dissapear), most likely you will try to restart your app which will allocate a new memory space potentially without freeing the previous one.
âtry to keep track of potential memory leaks wherever you can.
>> How to archive this goal? Add the "delete [] 'arr_name'" with every "new" array?â
yes, if you know that you wonât use the array anymore itâs a good practice to explicitly delete everything created with ânewâ.
you donât have to do it in stack as it is âtemporary spaceâ by design.
there is a quite good explanation of how does stack and heap works on YT.
https://youtu.be/wJ1L2nSIV1s?si=42e9nab2NpErFkjd&t=510
youâre interested in the part starting at approx 8:30 mark