Placing value on stack using alloca function or static keyword

94 Views Asked by At

I have an abstract "x vs y" question. In C programming language if I have some small amount of data which I want to store somewhere in RAM, I suppose typical options are the next ones:

  • to define a variable, which is going to be located in the "slow" heap unless some compiler optimisation;
  • to malloc() some space in the "slow" heap and to write here something;
  • to define a static variable, static keyword means related to stack, which means "fast" to me;
  • to allocate value in the "fast" stack using alloca() .

So what is the difference between static keyword and alloca() non-standard function?

1

There are 1 best solutions below

3
Ajay Brahmakshatriya On

First things first, your assumptions/understanding about the types of these variables are not accurate. Let's go through them one by one.

  1. By defining a variable, I assume you mean declaring it like int x;. The behavior of this depends on where such a declaration appears.

    i. If it is inside a function, it is what we call a local/auto variable. This would most likely be allocated on the stack (if it is not optimized away by the compiler).

    ii. If it appears at the file scope, it would be a global variable which would most likely be allocated in the global memory.

    Neither of these allocations are "slow" (if you compare it to the heap allocation costs)

  2. Calling malloc. This always allocates memory on the heap and returns a pointer to the allocated memory. Depending on how you define it, this could be slow since calling malloc executes code which figures out the space allocated on the heap. Memory allocated such a way must be free'd by calling free on the same pointer.

  3. Using the static keyword. static has nothing to do with the stack. When static appears inside a function, it is similar to making the variable global and will reside in the global memory. Using static at the file scope also makes the variable, but it is accessible only from the same file (as opposed to globals, which can accessed from other files as long as they have a declaration).

  4. Using alloca. Calling alloca is similar to calling malloc in the API but allocates space for you on the stack. This memory is never supposed to be manually free'd.

What you are missing is the other half of your question that determines what is the appropriate choice for you and that is the expected lifetimes of these objects.

Lifetime refers to how long the variables lives and would be valid to access. Let's go over the same 4 options and look at their lifetimes -

  1. The lifetime of a variable defined like int x; depends again on if it is local or global. If it is defined to be global, the lifetime is the entirety of the program and can be used any time. If defined locally in a function, the lifetime is till the end of the scope it is defined in (either the function call or within a block of {}). Be carefully taking addresses of such variables and not to access them after the scope they are defined in ends. (as suggested by @JonathanLeffler in comments)
  2. Memory allocated using malloc is accessible until you call free on the corresponding pointer. Which means the lifetime here is "dynamic".
  3. Variables with the static keyword have the same lifetime as globals in 1.
  4. Memory allocated using alloca has the same lifetime as variables defined locally like in 1. Once again be careful with the pointers and not to use them after the function returns.

Given all these choices and their tradeoffs, try to answer the two questions - 1. What is the expected lifetime and 2. Where do you want the variable to reside a.k.a., do you want to pay the cost of calling malloc and free and you will have your answer.