+ 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?

9th May 2024, 6:31 AM
Wong Hei Ming
Wong Hei Ming - avatar
3 Respuestas
+ 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.
9th May 2024, 8:02 AM
Dawid Galuszka
Dawid Galuszka - avatar
+ 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?
9th May 2024, 2:49 PM
Wong Hei Ming
Wong Hei Ming - avatar
+ 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
9th May 2024, 3:41 PM
Dawid Galuszka
Dawid Galuszka - avatar