I've been fine tuning a c++ program for years which does/did lots of mallocs and now its pretty good in the sense that its not spending much time on mallocs as opposed to the other "real" processing it has to do. I've literally spent many months optimising this program and performance and memory optimisation is absolutely critical.
I really would like to understand how the compiler/runtime will treat predeclared variables.
Lets take a simple function which uses a work area as a string
Now, if I declare that inside the function, and the function does not recurse, is C++ going to the use same memory buffer every call, or is it going to do a dynamic malloc on every function call, either of the 1024 bytes or of the block size needed for all variables declared in that function ? I'm thinking if its mallocing and freeing a block on every function call, it might be slowing things down.
If there's no recursion, the workarea could be declared outside of the function, and then just memset it to 0 on each call, and there's only one memory space used throughout the execution of the program. This is of course horrible programming, but if it gives me performance gains and memory optimisation, I'm up for it (and I can use static I suppose?)
Any advice and discussion around this topic would be much appreciated.. thanks