Can I declare an array of size n at runtime without using dynamic memory allocation (malloc etc.) in c?
-
2Welcome to SO. Yes, for local variables this is possible with C99. You can define an array `int arr[n]` in your function. Look up "Variable length array" (VLA) for more details. As this normally is allocated on the stack, you should take care not to allocate too much. The lifetime is limited to the function where you define that array. This is not possible for global variables. – Gerhardh Feb 07 '22 at 15:40
-
Just for clarification: Do you really mean without *dynamic memory allocation* or without *dynamic memory allocation on the heap*? I.e. do you want to allocate a runtime sized array on the stack? – TeeTrinker Jan 21 '23 at 22:43
3 Answers
By definition declaring a memory area of variable size at runtime is dynamic memory allocation, even if you don't use malloc
.
As Gerhardh said, you can use VLAs with some limitation. However you should use it with care as it may lead to stack overflow. See for example here.
There is also a library to allocate dynamic memory on the stack (look for alloca
). Its benefit is that you don't need any heap on your system to use it but it's not standard (AFAIK) and also to be used with care.
If you want to avoid heap dynamic allocation, it is probably because you are using a system with limited ressources. In this case, the stack is probably limited too and then you should consider defining statically allocated variables to avoid stack overflow.

- 2,408
- 1
- 21
- 47
Short answer: Just use malloc
, calloc
and free
unless you really can't computationally afford it.
Since C99, VLAs (variable-length arrays) were made standard:
int N;
scanf("%i", &N);
type Array[N]; //Size determined at runtime, grabbed from the stack
However, in the C11 standard they were made optional to implement, because their addition was widely criticized.
Major compilers implement them, but I'd discourage you to use them (unless as a temporary solution), because:
- Messy semantics with 2D arrays,
- Might blow up your stack (if you're not cautious),
Aditionally, VLAs are not a part of the C++ spec.
Don't get me wrong though, they have their uses in ex. embedded programming, where heap allocations (i.e. *alloc
-family functions) might be unreliable!
If you're using msvc, you can examine _malloca (and _alloca
), which perform explicit dynamically-sized-stack-allocation.
But think again if you really need these.
-
@EugeneSh. It's still worth mentioning if he ever wants to use C++ in the future. All of these points are only relevant in certain circumstances, so I don't see anything wrong with mentioning incompatibilities with C++. – Feb 07 '22 at 16:20
-
1
Just use malloc
, it's as easy as that:
#include <malloc.h>
int main(void)
{
const int n = 10; // Your array size
int* arr = (int*) malloc(n * sizeof(int));
// Access the pointer like an array
arr[0] = 100;
}
-
Downvote, because your answer is the exact thing OP wants to avoid. – TeeTrinker Jan 21 '23 at 22:37