First things first, your assumptions/understanding about the types of these variables are not accurate. Let's go through them one by one.
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)
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.
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).
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 -
- 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)
- Memory allocated using
malloc is accessible until you call free on the corresponding pointer. Which means the lifetime here is "dynamic".
- Variables with the
static keyword have the same lifetime as globals in 1.
- 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.
freeit?allocais allocating the space on the local stack, and will be gone after the current stack frame becomes invalid (function returns).staticvariables are allocated in the global data section and persist over the whole program run time. FWIW, the usecases forallocaare pretty limited, and personally I never used it explicitly (however it might be used implicitly by the compiler for VLAs and such).