0

For example, I created several dynamic arrays in the main function, will the system free them at the end, or do I need to delete them before RETURN ? I did not do this and did not notice problems, but my friend says it is necessary (he is a programmer by education), but for me it is strange, because it is very easy for the operating system to determine the memory that is no longer needed and delete everything. I am programming in c ++.

Kelbon
  • 29
  • 6
  • 2
    Assuming you're talking about an OS like Windows, etc., as opposed to some odd embedded concoction, anything allocated by that **process** should be freed. Threads are a different matter. – 3Dave Feb 23 '20 at 15:54
  • 2
    Does this answer your question? [C++ is it necessary to delete dynamically allocated objects at the end of the main scope?](https://stackoverflow.com/questions/10277817/c-is-it-necessary-to-delete-dynamically-allocated-objects-at-the-end-of-the-ma) and [Is there a reason to call delete in C++ when a program is exiting anyway?](https://stackoverflow.com/questions/677812/is-there-a-reason-to-call-delete-in-c-when-a-program-is-exiting-anyway) – walnut Feb 23 '20 at 15:55
  • Is this mean i can do absolutely everything in my MAIN and after return all memory will be clear? ( i expect YES ) but why my friend so dumb then with programming education? – Kelbon Feb 23 '20 at 15:57
  • 3
    The linked duplicates are under the assumption that you meant "*process*" when you wrote "*thread*". – walnut Feb 23 '20 at 15:57
  • 1
    One big reason to do it is to make sure your destructors are called. They could be closing connections, unlocking files, etc. Your friend isn't dumb - he's absolutely correct that you should explicitly free any dynamically-allocated resource. `main` is a bit of an edge case. – 3Dave Feb 23 '20 at 15:57
  • 2
    @Kelbon Please read the linked duplicates. While it is true that the OS will reclaim memory anyway, there are *other* reasons to call `delete` on dynamically allocated objects. – walnut Feb 23 '20 at 15:58

0 Answers0