I have a solution that actually worked for me. I did not want to allocate memory because of fragmentation on a routine that needed to run many times. The answer is extremely dangerous, so use it at your own risk, but it takes advantage of assembly to reserve space on the stack. My example below uses a character array (obviously other sized variable would require more memory).
void varTest(int iSz)
{
char *varArray;
__asm {
sub esp, iSz // Create space on the stack for the variable array here
mov varArray, esp // save the end of it to our pointer
}
// Use the array called varArray here...
__asm {
add esp, iSz // Variable array is no longer accessible after this point
}
}
The dangers here are many but I'll explain a few: 1. Changing the variable size half way through would kill the stack position 2. Overstepping the array bounds would destroy other variables and possible code 3. This does not work in a 64 bit build... need different assembly for that one (but a macro might solve that problem). 4. Compiler specific (may have trouble moving between compilers). I haven't tried so I really don't know.