Why global array has a larger size than the local array?

09 Jul, 2021
An array in any programming language is a collection of similar data items stored at contiguous memory locations and elements can be accessed randomly using array indices. It can be used to store the collection of primitive data types such as int, float, double, char, etc of any particular type. For example, an array in C/C++ can store derived data types such as structures, pointers, etc. Below is the representation of an array.
The arrays can be declared and initialized globally as well as locally(i.e., in the particular scope of the program) in the program. Below are examples to understand this concept better.
Program 1: Below is the C++ program where a 1D array of size 107 is declared locally.
C++
// C++ program to declare the array// of size 10^7 locally#include <bits/stdc++.h>using namespace std; // Driver Codeint main(){    const int N = 1e7;     // Initialize the array a[]    int a[N];    a[0] = 1;     cout << a[0];     return 0;}
Output:
Explanation: In the above program, a segmentation fault error occurs when a 1-D array is declared locally, then the limit of that array size is the order of 105. It is not possible to declare the size of the array as more than 105. In this example, the array of size 107 is declared, hence an error has occurred.
Program 2: Below is the program where a 1-D array of size 105 is initialized:
C++
Java
Python3
C#
Javascript
// C++ program to declare the array// of size 10^5 locally#include <bits/stdc++.h>using namespace std; // Driver codeint main(){    const int N = 1e5;     // Declare array a[]    int a[N];    a[0] = 1;     cout << a[0];     return 0;}
// Java program to declare the array// of size 10^5 locallyimport java.io.*; class GFG{ // Driver codepublic static void main(String[] arg){    int N = 1;         // Declare array a[]    int a[] = {N};    a[0] = 1;         System.out.print(a[0]);}} // This code is contributed by shivani
# Python program to declare the array# of size 10^5 locally# Driver codeN = 1e5   # Declare array a[]a = [N]a[0] = 1print(a[0]) # This code is contributed by shivanisinghss2110
// C# program to declare the array// of size 10^5 locallyusing System;class GFG{ // Driver codepublic static void Main(String[] arg){    int N = 1;         // Declare array a[]    int []a = {N};    a[0] = 1;         Console.Write(a[0]);}} // This code is contributed by shivanisinghss2110
<script> // Javascript program to declare the array// of size 10^5 locallyvar N = 1; // Declare array a[]var a = [N];a[0] = 1; document.write(a[0]); // This code is contributed by shivani </script>
1
 
Explanation: In the above program, the code compilation is successful, and the output is 1. This is because the 1D array of size 105 is initialized locally and this is valid.
Program 3: Below is the program where a 1-D array of size 107 is declared globally.
C++
Java
Python3
C#
Javascript
// C++ program to declare the array// of size 10^7 globally#include <bits/stdc++.h>using namespace std; // Variable N is initializedconst int N = 1e7; // Global array is declaredint a[N]; // Driver Codeint main(){    a[0] = 1;    cout << a[0];     return 0;}
// Java program to declare the array// of size 10^7 globallyimport java.io.*;  class GFG{  // Driver Codepublic static void main(String[] arg){    // Variable N is initialized    int N = 1;          // Global array is declared    int a[] = {N};       a[0] = 1;         System.out.print(a[0]);      }}// this code is contributed by shivani
# Python3 program to declare the array# of size 10^7 globally # Variable N is initializedN = 1e7 # Global array is declareda = [N] # Driver Codea[0] = 1print(a[0]) # This code is contributed by shivanisinghss2110
// C# program to declare the array// of size 10^7 globallyusing System;  class GFG{  // Driver Codepublic static void Main(String[] arg){    // Variable N is initialized    int N = 1;          // Global array is declared    int []a = {N};       a[0] = 1;         Console.Write(a[0]);      }}// this code is contributed by shivanisinghss2110
<script> // Javascript program to declare the array// of size 10^7 globally // Variable N is initializedconst N = 1e7; // Global array is declaredlet a = new Array(N); // Driver Codea[0] = 1;document.write(a[0]); // This code is contributed by gfgking.</script>
1
 
Explanation: In the above code compilation is successful, and the output is 1. This is because a 1-D array of size 107 is declared globally and this is valid. 
Note: If a 1-D array of size 108 is declared globally, then again the segmentation fault error will be encountered because there is also a limit for global declaration of the 1-D array and that is, it is possible to only declare a 1-D array globally up to 107 sizes.
Why global array has a larger size than the local array?
When an array is declared locally, then it always initializes in the stack memory, and generally, stack memory has a size limit of around 8 MB. This size can vary according to different computer architecture.
When an array is declared globally then it stores in the data segment, and the data segment has no size limit. Hence, when the array is declared of big size (i.e., more than 107) then the stack memory gets full and leads into the stack overflow error, and therefore, a segmentation fault error is encountered. Therefore, for declaring the array of larger size, it is good practice to declare it globally.
simranarora5sos
shivanisinghss2110
gfgking
C Basics
CPP-Basics
Arrays
C++
C++ Programs
Arrays
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.