Say that I have the following situation:void myFunc(){    int x;    //Do something with x}x is placed on the stack which is no doubt fast.Now, myFunc is called very frequently, lets say 10 times per second.Is it plausible to do something like this:int x;void myFunc(){    //Do something with x}so that x gets allocated in the applications data segment. but it is allocated only once. Since myFunc is called so frequently, does the second approach deliver any performance benefits?
Place variables frequently used by the same function on the heap?
c;memory;stack;heap
Allocating a variable on the stack and deallocating it is a simple addition and subtraction of the stack pointer. Given that it happens anyway when entering a function means that local variables are so cheap that trying to optimize them to anything else will generally incur more cost.Putting it in the data segment will incur a cache cost, the stack will usually be in cache.The biggest disadvantage is that you lose reentrant properties. Meaning that recursion won't work and that it's not thread-safe.The other big disadvantage is that letting the variable escape scope is that the optimizer can't make it a register-only variable, which never reaches RAM to begin with.