0

I am trying to find the difference between static and dynamic memory allocation...

This is static memory allocation:

#include<stdio.h> int main(){ int a; printf("Enter the size of array: "); scanf("%d",&a); int arr[a]; for(int i=0;i<a;i++){ arr[i]=i; } for(int i=0;i<a;i++){ printf("%d\n",arr[i]); } return 0; } 

Output:

/tmp/aGaOu4PWEU.o Enter the size of array: 10 0 1 2 3 4 5 6 7 8 9 

When we compile and run this we have to enter value of 'a' during run time and size of array arr[] is also set during run time, but how is it compile time???

6
  • You are studying C. Better leave C++ out of your question. Commented Nov 5, 2022 at 8:17
  • What is static memory allocation? I don't think there's any such thing in C, although there's a bunch of related concepts -- static storage duration, knowing the size of an object at compile-time, and allocation on the stack. Commented Nov 5, 2022 at 8:28
  • "static memory allocation" is a non-sequitur if it means compile-time allocation because all memory allocation is done at program load-time, or program run-time, either implicitly or explicitly. Commented Nov 5, 2022 at 8:30
  • 1
    @PaulHankin: Allocation is reserving memory. For objects with static storage duration, it happens before or as the program starts. The C standard calls dynamically allocated memory “allocated storage,” but it also uses “allocated” in the sense above. “Static memory allocation” is simply allocation of memory for objects of static storage duration. Commented Nov 5, 2022 at 10:13
  • what you do here is a Variable Length Array. Commented Sep 26, 2024 at 13:10

3 Answers 3

1

enter image description here

There is said to have 3 types of memory allocations:

Static allocation, Dynamic allocation and Automatic/Stack allocation

Static allocation:

Memory is allocated during compile time. Variables with global scope or declared with the static keyword fall under this category. The size and address of the variable are determined before the program runs, and the memory is reserved throughout the program's lifetime.

Dynamic allocation:

Memory is allocated at runtime, typically from the heap using functions like malloc(), calloc(), or realloc(). This memory must be manually managed by the programmer.

Automatic/Stack Allocation:

Local variables (like i in your example) are allocated at runtime when the function is called but on the stack. This is sometimes referred to as automatic allocation because memory is automatically freed when the function returns. However, this isn't the same as dynamic memory allocation in the sense of the heap.

All those variables with static storage duration (ie., static variables and global variables) will be allocated memory inside static memory. Thier sizes are known during compilation itself. For further clarifications, check this

However, although the auto variables are allocated automatically at runtime into the stack, the stack's total size is fixed at the compile time, in most cases. This means, the total stack size is fixed at compile time through compiler options or system settings. But the automatic memory allocation will be happening at the runtime only, when the corresponding function is called. Thus eventhough the stack grows downwards, the total size allocated for stack is fixed at compile time and once the limit is reached, further attempts to push the data into the stack, will lead to stack overflow.

Now, your question's direct answer is, when a function with a VLA is called, the size of the array is computed at runtime, and the stack pointer is adjusted to allocate the required space. The stack frame is expanded dynamically to accommodate the size of the VLA.Once the function returns, the stack pointer is adjusted to free the space, just like with regular local variables.

Limitations of VLA:

VLAs can increase the risk of stack overflow, especially if a large VLA size is provided at runtime, because the total stack size is still limited (usually a few MBs). Unlike heap allocation (which is much larger), the stack has relatively small limits.

Your example comes under automatic memory allocation (auto variable with runtime memory allocation in stack).

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

Comments

0

What you are using here is a variable length array (VLA for short). It is allocated during runtime, but it is allocated on the stack.

I believe you may confuse static allocation and stack allocation. Static allocation is only possible if the compiler knows the object size at compile time.

Comments

-1

A static array is located in a continuous segment of memory, sizeof(int) bytes are allocated for each element of the array, respectively, the amount of memory required to accommodate the entire array is a*sizeof(int) bytes according to your code. This value is limited from above by the platform and the compiler. If an array is declared statically, that is, in the global scope, in the scope of a namespace, or as a static member of a class, then it is allocated in static memory. Arrays declared locally are allocated memory on the stack, which has a limited size. Dynamic arrays are allocated in dynamic memory. For dynamic arrays, there are only operations for creating and deleting them; to work with such arrays, pointers to their beginning are used. The size of such an array must be stored separately, sometimes pointers to the null element are used for this. If a static array is defined as a global variable (i.e., declared outside the body of any function, or with the static modifier), then its size is added to the total size of all uninitialized global variables of the program, the memory for these variables is allocated by the system entirely at the stage downloads. Therefore, the sizes of such arrays can no longer be changed, the compiler needs to know them in advance, which means that only constants are allowed as them.

4 Comments

This answer is not completely wrong, but it is a large run-on paragraph with some misleading and inappropriate things. Re “A static array is located in a continuous segment of memory”: Any array is a contiguously allocated sequence of objects. Re “This value is limited from above by the platform and the compiler”: The meaning of “limited from above” is not clear. Re “in the global scope”: C does not have a global scope. It has file scope and external linkage. Re “the scope of a namespace”: C does not have this kind of namespace; that is C++.
Re “Arrays declared locally are allocated memory on the stack”: This is not always true, since declarations with static or extern do not result in an object with automatic storage duration. Re “Dynamic arrays are allocated in dynamic memory”: “Dynamic arrays” is an ambiguous term; C has variable length arrays which may be called dynamic, but they are not necessarily in the “allocated” storage class.
Re “For dynamic arrays, there are only operations for creating and deleting them”: What? There are operations for accessing the elements of arrays. Re “The size of such an array must be stored separately, sometimes pointers to the null element are used for this”: The size must be managed somehow (which is not necessarily by explicitly storing it separately). Arrays do not have a “null element” in the sense the C standard uses “null,” except possibly for arrays of pointers.
Re “the memory for these variables is allocated by the system entirely at the stage downloads”: The meaning of “the stage downloads” is not evident.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.