0

Can I declare an array of size n at runtime without using dynamic memory allocation (malloc etc.) in c?

2
  • 2
    Welcome 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. Commented Feb 7, 2022 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? Commented Jan 21, 2023 at 22:43

3 Answers 3

4

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.

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

Comments

3

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:

  1. Messy semantics with 2D arrays,
  2. 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.

2 Comments

@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++.
@EugeneSh. Fixed.
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;
}

1 Comment

Downvote, because your answer is the exact thing OP wants to avoid.

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.