TheHingineer

  • Data Structures & Algorithms


  • DSA Part-1

  • DSA Part-2

  • DSA Part-3

  • DSA Part-4

  • DSA Part-5

  • 1D Arrays: Memory Representation and Address Calculation

    1D Array (One-Dimensional Array) ek linear data structure hota hai jisme same data type (homogeneous) ke elements computer memory ke andar continuous (contiguous) memory blocks me store hote hain. Array me har element ko uske unique Index (Subscript) dwara directly access kiya ja sakta hai.

    Key Characteristics of 1D Arrays

    • Homogeneous Data: Array ke saare elements same data type (jaise saare integer ya saare float) ke hote hain.
    • Contiguous Allocation: Memory me elements bagal-bagal (adjacent blocks) me place hote hain.
    • Random Access [O(1)]: Kisi bhi element ka address direct calculation se mil jata hai, isliye indexing me constant time lagta hai.
    • Fixed Size: Compile-time par define kiya gaya size runtime par automatically expand ya shrink nahi ho sakta (static memory allocation).

    Memory Layout of 1D Array

    Agar ek integer array int arr[5] declare kiya jaye, jahan har integer 4 bytes memory leta hai, aur pehle element ka starting memory address Base Address (B) = 1000 hai:

    Index:           0          1          2          3          4
    Element:      +----+     +----+     +----+     +----+     +----+
                  | 25 |     | 40 |     | 12 |     | 88 |     | 95 |
                  +----+     +----+     +----+     +----+     +----+
    Address:       1000       1004       1008       1012       1016
                   ^ Base Address (B)
                   |<-- W=4 -->|
    

    1D Array Address Calculation Formula

    University exam numericals me kisi specific index i par rakhe element ka physical memory address calculate karne ka standard formula pucha jata hai:

    Address of A[i] = Base(A) + W * (i – Lower_Bound)

    Yahan parameters ka meaning:

    • Base(A) / B: Array ke first element ka base memory address.
    • W: Har single element ka memory size (in bytes/words). E.g., int ke liye 2 ya 4 bytes, float ke liye 4 bytes.
    • i: Us element ka index jiska address calculate karna hai.
    • Lower_Bound (LB): Array ka starting index (C/C++/Java me LB = 0 hota hai, lekin numericals me custom lower bound jaise A[-5 ... 15] diya ja sakta hai).

    Length / Total Elements Calculation:

    Length = Upper_Bound (UB) - Lower_Bound (LB) + 1

    Solved University Numerical

    Question: Ek array A[10 ... 50] diya gaya hai jiska Base Address 1500 hai aur har element memory me 4 bytes leta hai. Element A[32] ka memory address calculate kijiye.

    Solution:

    • Base Address (B) = 1500
    • Element Size (W) = 4 bytes
    • Target Index (i) = 32
    • Lower Bound (LB) = 10

    Formula apply karne par:

    Address(A[32]) = Base + W * (i - LB)
                   = 1500 + 4 * (32 - 10)
                   = 1500 + 4 * (22)
                   = 1500 + 88
                   = 1588

    Toh element A[32] ka physical memory address 1588 hoga.

    Agli post me hum cover karenge 2D Arrays: Row-Major and Column-Major Order Formulas and Numericals.

    Scroll to Top