Is this is an example of static memory allocation or dynamic memory allocation?
Asked Answered
I

3

5

I researched a lot of static and dynamic memory allocation but still, there is a confusion that:

int n, i, j;
printf("Please enter the number of elements you want to enter:\t");
scanf("%d", &n);
int a[n];
for (i = 0; i < n; i++)
{
    printf("a[%d] : ", i + 1);
    scanf("%d", &a[i]);
}

Does int a[n] come under static or dynamic memory allocation?

Instal answered 12/12, 2017 at 21:30 Comment(6)
What do you think? How would you define dynamic allocation?Certitude
I think maybe dynamic memory allocation.Instal
Well, although one can claim that it is dynamic, since the amount of the memory allocated is decided in the run-time, it is not a dynamic allocation in the strict C meaning, which has a distinction between static, automatic and dynamic allocation. This one is falling under "automatic".Certitude
@EugeneSh. - To be pedantic, C distinguishes storage durations - static vs. automatic vs. allocated (vs. thread in C11).Dissentient
It's a good question -- dynamic allocation in C almost always refers to the malloc-family of functions and heap allocation, but this is definitely not static allocation, so it's not weird that you are confused with how to clasify it. So, yes, this falls under the third type, "automatic allocation". In this case a is a variable-length automatic array. Note that the actual place where this array will be stored is implementation dependant, C does not deal with stacks and heaps.Font
And the distinction between "allocation" and "storage duration" is key here, in that it underscores that C is concerned with how the memory is managed -- its whole life cycle -- not just how and when it is obtained. If one is concerned only with the latter part, then this is a matter of how you define your terms.Farah
W
14

The C standard does not talk about dynamic allocation (or static allocation, for that matter). But it does define storage durations: static, automatic, thread and allocated. Which dictates how long an object (a piece of data) lives (is usable).

  • Static in the sense of storage duration means the object is usable for the entire execution of the program. Variables at file scope ('global' variables), and local variables with static in their declaration, have static storage duration.

  • Automatic storage duration are your regular local variables, they live only for the duration of the block they are declared in (the function, or inside the curly braces of e.g. a for loop).

  • Allocated storage duration refers to memory obtained via malloc and friends. It is available from a (successful) call to malloc, until a corresponding call free. This is often referred to as dynamic memory allocation, as it is a way to obtain a block of memory with the size determined at run-time.

Your variable a has automatic storage duration. However, it can be considered dynamic in the sense that its length is determined at run-time, rather than compile-time. Just like allocated storage duration has.

Waylonwayman answered 12/12, 2017 at 21:42 Comment(0)
P
3

int a[n] is a variable length array with the automatic storage duration,

Consider the following demonstrative program.

#include <stdio.h>
#include <string.h>

int main(void) 
{
    const size_t N = 10;

    for ( size_t i = 1; i <= N; i++ )
    {
        char s[i];

        memset( s, '*', sizeof( s ) );

        printf( "%*.*s\n", ( int )i, ( int )i, s );
    }

    return 0;
}

Its output is

*
**
***
****
*****
******
*******
********
*********
**********

Each time when the control is passed to the body of the loop the generated by the compiler code creates a local array s[n] life-time of which ends at the end of the body of the loop.

Pavonine answered 12/12, 2017 at 21:35 Comment(0)
W
1

No it's comes under automatic allocation

Waxman answered 17/12, 2017 at 2:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.