Skip to main content

Static vs Dynamic Array

 Static and dynamic array sizes refer to the ways in which the size of an array is determined and managed. Here's an explanation of each:


1. Static Array Size:

   In a static array, the size is fixed and determined at compile time. The size of a static array is specified when the array is declared, and it remains constant throughout the program's execution. The compiler allocates memory for the array based on its declared size. Static arrays have a fixed amount of memory reserved for them, which cannot be changed during runtime.


   Here's an example of a static array declaration in C++:


   cpp

   int numbers[5];  // Static array of size 5

   


   In this case, the array `numbers` has a static size of 5 integers. The size cannot be modified once the array is declared.


   Static arrays have the advantage of simplicity and efficiency in terms of memory access, as the memory for the entire array is allocated in a single block. However, their fixed size can be a limitation when you need to store a varying number of elements or when the size needs to be determined dynamically.


2. Dynamic Array Size:

   In contrast to static arrays, dynamic arrays have a size that can be determined and modified during runtime. Dynamic arrays are typically implemented using pointers and dynamic memory allocation. The size of a dynamic array can be specified by the programmer or determined based on user input or other factors at runtime.


   Here's an example of a dynamic array allocation in C++ using the `new` operator:


   cpp

   int* numbers = new int[5];  // Dynamic array of size 5

 


   In this case, the `new` operator is used to allocate memory for the dynamic array of 5 integers. The size can be changed later by deallocating the array and allocating a new one with a different size.


   Dynamic arrays provide flexibility in handling varying or unknown numbers of elements. They can be resized and reallocated as needed, allowing for efficient memory utilization. However, managing dynamic arrays requires careful memory management to avoid memory leaks and deallocation issues. It's important to release the memory using the `delete[]` operator when the array is no longer needed to prevent memory leaks.


   Static Vs Dynamic Arrays

 • Static array means the size of an array is static, I.e; fixed

 • Dynamic array means the size of an array is Dynamic, I.e; flexible 

• When an array is created, it is created inside Stacking the memory 

• The size of the array is decided during at compile time

 • When declaring an array it must be a static value only and not variable type in c language however in c++ dynamic allocation is possible during compile time We can create array inside Heap

 • When accessing any value inside a heap it must be done through a pointer

• When the work in heap is done it must be deleted or it will cause memory leak which will cause problem

 • To release the heap memory we do c++ 

delete[ ] p; 

C lang free( p );

Example-:

C Language

#include<stdio.h>

#include<stdlib.h>

int main(){

   int A[5]={1,3,5,7,9,11};//static Array

   int* p=(int *)malloc(5*sizeof(int));//dynamic Array

   p[0]=2;

   p[1]=4;

   p[2]=6;

   p[3]=8;

   p[4]=10;

   for(int i=0;i<5;i++){

        printf("%d",A[i]);

   }

  for(int i=0;i<5;i++){

        printf("%d",p[i]);

}

return 0;

}

       

C++ 

#include <iostream>

int main() { 

    int size = 5;

    Int A[5]={1,2,4,5,6};//create a static array

    // Create a dynamic array of integers

    int* numbers = new int[size];


    // Assign values to array elements

    for (int i = 0; i < size; i++) {

        numbers[i] = i + 1;

    }

    // Access and print array elements

    for (int i = 0; i < size; i++) {

        std::cout << numbers[i] << " ";

    }

    std::cout << std::endl;


    // Deallocate the dynamic array

    delete[] numbers;


    return 0;

}





In Java

public class DynamicArrayExample {

    public static void main(String[] args) {

        // Create a dynamic array of integers

        int size = 5;

        int[] numbers = new int[size];


        // Assign values to array elements

        for (int i = 0; i < size; i++) {

            numbers[i] = i + 1;

        }


        // Access and print array elements

        for (int i = 0; i < size; i++) {

            System.out.print(numbers[i] + " ");

        }

        System.out.println();

    }

}




Popular posts from this blog

Java

Codes With Java — Basics Codes With Java Java tutorials & fundamentals About Contact Privacy Basic Fundamentals Java source file structure Import Statement Static Import Packages Data Type Variables Final Variable Declaration and Access Modifier Inner classes applicable modifiers Static Modifier Synchronized Native Transient Volatile Interface Introduction Interface Declaration and Implementation Interface methods and variables Naming Conflicts Interface Marker interface and Ad...

Short Circuite Operators part 4

                                                             Short Circuit Operators In  Java logical operators , if the evaluation of a logical expression exits in between before complete evaluation, then it is known as  Short-circuit . A short circuit happens because the result is clear even before the complete evaluation of the expression, and the result is returned. Short circuit evaluation avoids unnecessary work and leads to efficient processing. 1-: AND(&&) 2-:OR(||) these are exactly same as bitwise operators (&,|) except the following differences. Single Short Circuit Operator(&,|) Both arguments Should be evaluated always. relatively performance is low. Applicable for both boolean and Integral types. Double Short Circuit Operator(...

Operators & Assignment part 3

                                                                                       Operators & Assignment 1-:instanceof Operators 2-:Bitwise Operators                                                                        instanceof Operators we can use instanceof operator to check whether the given object is of a particular type are not. Example-: /* list is a collection of objects. List l=new List(); l.add(Customer); l.add(Student); l.add(Test); Object o=l.get(...