Skip to content
Related Articles

Related Articles

Improve Article

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

  • Difficulty Level : Medium
  • Last Updated : 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.

Array

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

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 Code
int 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++




// C++ program to declare the array
// of size 10^5 locally
#include <bits/stdc++.h>
using namespace std;
 
// Driver code
int main()
{
    const int N = 1e5;
 
    // Declare array a[]
    int a[N];
    a[0] = 1;
 
    cout << a[0];
 
    return 0;
}

Java




// Java program to declare the array
// of size 10^5 locally
import java.io.*;
 
class GFG{
 
// Driver code
public 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

Python3




# Python program to declare the array
# of size 10^5 locally
# Driver code
N = 1e5
   
# Declare array a[]
a = [N]
a[0] = 1
print(a[0])
 
# This code is contributed by shivanisinghss2110

C#




// C# program to declare the array
// of size 10^5 locally
using System;
class GFG
{
 
// Driver code
public 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

Javascript




<script>
 
// Javascript program to declare the array
// of size 10^5 locally
var N = 1;
 
// Declare array a[]
var a = [N];
a[0] = 1;
 
document.write(a[0]);
 
// This code is contributed by shivani
 
</script>
Output: 
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++




// C++ program to declare the array
// of size 10^7 globally
#include <bits/stdc++.h>
using namespace std;
 
// Variable N is initialized
const int N = 1e7;
 
// Global array is declared
int a[N];
 
// Driver Code
int main()
{
    a[0] = 1;
    cout << a[0];
 
    return 0;
}

Java




// Java program to declare the array
// of size 10^7 globally
import java.io.*;
  
class GFG
{
 
 
// Driver Code
public 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




# Python3 program to declare the array
# of size 10^7 globally
 
# Variable N is initialized
N = 1e7
 
# Global array is declared
a = [N]
 
# Driver Code
a[0] = 1
print(a[0])
 
# This code is contributed by shivanisinghss2110

C#




// C# program to declare the array
// of size 10^7 globally
using System;
  
class GFG
{
 
 
// Driver Code
public 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

Javascript




<script>
 
// Javascript program to declare the array
// of size 10^7 globally
 
// Variable N is initialized
const N = 1e7;
 
// Global array is declared
let a = new Array(N);
 
// Driver Code
a[0] = 1;
document.write(a[0]);
 
// This code is contributed by gfgking.
</script>
Output: 
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.



My Personal Notes arrow_drop_up
Recommended Articles
Page :