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,
statickeyword 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?
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 callingmallocexecutes code which figures out the space allocated on the heap. Memory allocated such a way must be free'd by callingfreeon the same pointer.Using the
statickeyword.statichas nothing to do with the stack. Whenstaticappears inside a function, it is similar to making the variable global and will reside in the global memory. Usingstaticat 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. Callingallocais similar to callingmallocin the API but allocates space for you on the stack. This memory is never supposed to be manuallyfree'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 -
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)mallocis accessible until you callfreeon the corresponding pointer. Which means the lifetime here is "dynamic".statickeyword have the same lifetime as globals in 1.allocahas 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
mallocandfreeand you will have your answer.