Table of Contents

    Limitations of Arrays - Fixed Size

    Limitations of Arrays

    Arrays are one of the most commonly used data structures in programming. They are simple, fast, and easy to use. However, arrays also have several important limitations. One of the biggest limitations is their fixed size.


    1. Fixed Size

    In the C programming language, the size of an array must usually be specified at the time of declaration.

    
    int arr[100];
    

    The above statement creates an array capable of storing exactly 100 integer values.

    Once the array is created, its size becomes fixed during program execution. This means:

    • You cannot increase the size dynamically.
    • You cannot easily reduce the size.
    • The memory allocated for the array remains reserved.

    Problem 1: Array Becomes Full

    Suppose a program stores marks of students using an array:

    
    int marks[5];
    

    This array can store only 5 student marks.

    Let us assume the array currently contains:

    
    marks[0] = 75;
    marks[1] = 80;
    marks[2] = 68;
    marks[3] = 90;
    marks[4] = 85;
    

    The array is now completely full.

    Index Value
    0 75
    1 80
    2 68
    3 90
    4 85

    Now suppose a new student joins the class and we want to store one more mark:

    
    marks[5] = 95;
    

    This creates a problem because the array size is only 5. Valid indexes are:

    
    0 to 4
    

    Trying to access index 5 may produce:

    • Undefined behavior
    • Program crash
    • Memory corruption

    Therefore, adding new elements becomes difficult once the array becomes full.

    "In C language, accessing an array outside its valid index range causes undefined behavior. The compiler may not generate an error because C does not perform automatic array bounds checking.

    Important Interview/Exam Point

    Array overflow in C:

    • May not produce compile-time error
    • May not produce runtime error
    • Still considered dangerous and incorrect programming practice


    C Program Example: Array Overflow Problem

    
    #include < stdio.h >
    
    int main()
    {
        int marks[5] = {75, 80, 68, 90, 85};
    
        /* Trying to insert extra value */
        marks[5] = 95;
    
        printf("Value: %d", marks[5]);
    
        return 0;
    }
    

    The above program attempts to insert a value beyond the array boundary, which is unsafe in C programming.


    Problem 2: Memory Wastage

    Sometimes programmers allocate very large arrays to avoid overflow problems.

    
    int data[1000];
    

    Suppose the program actually uses only 50 elements.

    Then:

    
    1000 - 50 = 950
    

    memory locations remain unused.

    This unused memory is called memory wastage.


    Visualization of Memory Wastage

    Total Array Size Used Elements Unused Elements
    1000 50 950

    Even though the unused memory is not storing useful data, it still occupies RAM.


    C Program Example: Memory Wastage

    
    #include < stdio.h >
    
    int main()
    {
        int data[1000];
    
        data[0] = 10;
        data[1] = 20;
        data[2] = 30;
    
        printf("Only few elements are used.");
    
        return 0;
    }
    

    In this example:

    • The array can store 1000 integers.
    • Only 3 elements are being used.
    • The remaining memory remains unused.

    Why This Creates Problems

    Due to fixed size limitations:

    • Programs become less flexible.
    • Efficient memory utilization becomes difficult.
    • Dynamic data handling is not possible easily.
    • Large applications may suffer from memory management issues.

    To solve these problems, dynamic data structures like Linked Lists were introduced.