sidharth ramanan
sidharth ramanan

Reputation: 321

How can I calculate/approximate the memory used by a stack frame of a recursive function?

I want to be able to calculate, or at least approximate how much memory, in bytes or whatever, a particular stack frame of a recursive function will use, based on the parameters of the recursive function.

For example, how much memory might the stack frame of a recursive function like sample_recursive_function(int [] array, int n) take?

Upvotes: 2

Views: 1147

Answers (1)

PMF
PMF

Reputation: 17298

You can approximate that with the size of the arguments plus a constant return address size. For your example, it is small: int[] is a pointer type (for java, C++ or C#) and uses 4 or 8 bytes (depending on whether the code runs on 32 or 64 bit), int is 4 bytes and the return addresss is also 4 or 8 bytes. So the optimized stack frame for this function will be 12 bytes on 32 bit and 32 bytes (due to padding) on 64 bit. When running in debug mode, some extra bytes might be used. Also, any local variables in the function will also add to the size of the stack frame. Details might be a bit more complex.

Upvotes: 2

Related Questions