Home > Enterprise >  What is the difference in declaring a array inside the int main() and outside the int main()?
What is the difference in declaring a array inside the int main() and outside the int main()?

Time:08-26

CASE 1

#include <stdio.h>

long long arr[10000005];

int main()
{
    int n;
    int m;

    scanf("%d %d", &n, &m);
    int index1, index2, summand;
    for (int i = 0; i < n; i  )
    {
        arr[i] = 0;
    }
    while (m--)
    {
        scanf("%d %d %d", &index1, &index2, &summand);

        arr[index1-1]  = summand;
        arr[index2] -= summand;
    }

    long long max = 0, temp = 0;
    for (int i = 0; i < n; i  )
    {
        temp  = arr[i];
        if (temp >= max)
        {
            max = temp;
        }
    }
    printf("%lld", max);
}

CASE 2

#include <stdio.h>

int main()
{
    int n;
    int m;
    long long arr[10000005];
    scanf("%d %d", &n, &m);
    int index1, index2, summand;
    for (int i = 0; i < n; i  )
    {
        arr[i] = 0;
    }
    while (m--)
    {
        scanf("%d %d %d", &index1, &index2, &summand);

        arr[index1-1]  = summand;
        arr[index2] -= summand;
    }

    long long max = 0, temp = 0;
    for (int i = 0; i < n; i  )
    {
        temp  = arr[i];
        if (temp >= max)
        {
            max = temp;
        }
    }
    printf("%lld", max);
}

This is the code for array manipulation problem in hackerrank. In the first case the array was declared inside the int main(), But the code execution ended with segmentation fault. In the Second case, the array was declared outside the int main(). Then the code executed with no error msgs through all the test cases.

CodePudding user response:

The storage duration is different:

  • An array created with long long arr[10000005]; outside of any function exists for the entire duration of program execution. This is called static storage duration.
  • An array created with long long arr[10000005]; inside a function exists for the duration of execution of the block it is in. This is called automatic storage duration.

The amount of space available is different:

  • Memory for an array with static storage duration is arranged when linking and loading the program, and the loading process makes enough memory available for the array, subject to system limits.
  • Memory for an array with automatic storage duration uses the stack in typical C implementations, and the stack size is usually limited to a few megabytes (more if you make special provisions when building the program, typically less in kernel software and embedded systems).

The scope of the name is different:

  • For an array declared outside of any function, its name can be used to refer to that array for the rest of the translation unit (the source code file being compiled, including any files it includes), unless the name is hidden by additional declarations inside nested scopes.
  • For an array declared inside a function, its name can be used to refer to that array for the rest of the block it is in (again unless it is hidden by additional declarations).

The linkage of the name is different:

  • For an array declared outside of any function, its name has external linkage, meaning that uses of that name in other translation units (or different scopes in the same unit) can be made to refer to the same array when the program is linked.
  • For an array declared inside a function, its name has no linkage, meaning that uses of that name in other translation units (or scopes) cannot be made to refer to the same array.

CodePudding user response:

When you declared the array outside the int main() function, it became a global variable. This means that you can access it in any function, not just int main(). When you declared the array inside the int main() function, it became a local variable. You can still access it in the main(), but you cannot access it directly in any other function without using a pointer.

  • Related