How to know how much stack function is consuming?

后端 未结 4 1678
天涯浪人
天涯浪人 2021-01-22 06:04

Recently, I came across this question in an interview:
How can we determine how much storage on the stack a particular function is consuming?

4条回答
  •  借酒劲吻你
    2021-01-22 06:40

    The size increase of the stack, for those implementations that use a stack, is:

    • size of variables that don't fit in the available registers
    • size of variables declared in the function declared upfront that live for the life of the function
    • size of other local variables declared along the way or in statement blocks
    • the maximum stack size used by functions called by this function
    • everything above * the number of recursive calls
    • size of the return address

    Return Address

    Most implementations push the return address on the stack before any other data. So this address takes up space.

    Available Registers

    Some processors have many registers; however, only a few may be available for passing variables. For example, if the convention allows for 2 variables but there are 5 parameters, 3 parameters will be placed on the stack.

    When large objects are passed by value, they will take up space on the stack.

    Function Local Variables

    This is tricky to calculate, because variables may be pushed onto the stack and then popped off when not used.

    Some variables may not be pushed onto the stack until they are declared. So if a function returns midway through, it may not use the remaining variables, so the stack size won't increase for those variables.

    The compiler may elect to use registers to hold values or place constants directly into the executable code. In this case, they don't add any length to the stack.

    Calling Other Functions

    The function may call other functions. Each called function may increase the amount of data on the stack. Those functions that are called may call other functions, and so on.

    This again, depends on the snapshot in time of the execution. However, one can produce an approximate maximum increase of the stack by the other called functions.

    Recursion

    As with calling other functions, a recursive call may increase the size of the stack. A recursive call at the end of the function may increase the stack more than a recursive call near the beginning.

    Register Value Saving

    Sometimes, the compiler may need more space for data than the allocated registers allow. Thus the compiler may push variables on the stack.

    The compiler may push registers on the stack for convenience, such as swapping registers or changing the value's order.

    Summary

    The exact size of stack space required for a function is very difficult to calculate and may depend on where the execution is. There are many items to consider in stack size calculation, such as parameter quantity and size as well as any other functions called. Due to the variability, most stack size measurements are based on a maximum size, or worst case size. Stack allocation is usually based on the worst case scenario.

    For an interview question, I would mention all of the above, which usually makes the interviewer want to move on to the next question quickly.

提交回复
热议问题