-3

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?

4
  • 4
    Besides the fact that these assumptions are all wrong, have you decide on the lifetime of the object? Do you want it to last for the entirety of the program? Or the duration of a function call? Or until you manually free it? Commented Jan 26, 2024 at 17:16
  • Only the last question makes some sense. alloca is allocating the space on the local stack, and will be gone after the current stack frame becomes invalid (function returns). static variables are allocated in the global data section and persist over the whole program run time. FWIW, the usecases for alloca are pretty limited, and personally I never used it explicitly (however it might be used implicitly by the compiler for VLAs and such). Commented Jan 26, 2024 at 17:22
  • Said that, you do not typically decide on the storage class base on this "slow/fast" distinction, but based on the required lifetime of the data, it's required visibility/scope and sometimes it's size. Commented Jan 26, 2024 at 17:27
  • @Eimrine, Concerning fast versus slow: Is premature optimization really the root of all evil?. Commented Jan 26, 2024 at 18:31

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Note that MS Visual C's implementation of alloca() is deprecated in favour of their _malloca() which uses the heap or stack as appropriate and must always be paired with a call to _freea().
I would use the correct working "static storage duration", "automatic storage duration", "allocated storage duration" & "thread storage duration"
Re “it is accessible only from the same file”: All objects are accessible (can be read or written) from anywhere in a program. Pointers can be used for this. static limits scope and linkage, not accessibility: The name of an object, its identifier, is not known (usable to reference the object) outside its scope, and static limits its linkage to internal linkage.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.