Something that has been puzzling me for a while in C++, when I learned how to dynamically allocate memory (using new and delete etc.) I started using it for almost all my variables in my projects, but I just started to question why. What is the point of statically allocating memory if it less flexible and more difficult to control?
1) performance. Locally allocated variables are quicker to access than having to dereference a pointer to dynamically allocated memory.
2) Cleanup and maintenance. locally allocated variables dont' need to be deleted and therefore have zero risk of leaking memory.
3) memory fragmentation. contantly allocating and deleting small blocks of memory can cause memory to become fragmented, impacting performance and overall program memory usage over a long period of time. Locally allocated variables simply consume stack space which has been preallocated so there's no possibility of fragmentation.
You actually should be doing the exact opposite of what you are. Only use new if you have to. Otherwise try to avoid it.