Data for vectors is always dynamically allocated. Only accounting data is stored on the stack. Even if it weren’t, the memory allocation on the stack was essentially free. Allocation from the stack simply changes the value of the register on most architectures.
EDIT
As for dynamic release, it must be released at one point or another (in particular, at the end of the function). You do not lose anything by leaving the allocated memory until you want to allocate more, and this is not enough. Is the exact time when this release happens, really need to bother something before you encounter any problem?
/ EDIT
But what's the point? It seems that you are prematurely treating yourself with optimization.
If you want to reorganize your code, do it for the sake of clarity, not performance.
source share