Arrays in C++

Arrays in C++ are fundamental data structures that store elements of the same type in contiguous memory. Here's a structured overview:

 **1. Declaration and Initialization**
- **Static Arrays**: Fixed size determined at compile-time.
  ```cpp
  int arr[5];             // Uninitialized array of 5 integers
  int arr[] = {1, 2, 3};  // Size inferred as 3
  int arr[5] = {1, 2};    // Initialized to {1, 2, 0, 0, 0} (remaining elements zeroed)
  ```

- **Dynamic Arrays**: Size determined at runtime using `new`/`delete`.
  ```cpp
  int size = 10;
  int* arr = new int[size]; // Allocate memory
  delete[] arr;             // Deallocate to avoid leaks
  ```
 **2. Access and Iteration**
- Zero-indexed, accessed via `[]`.
- Use loops to iterate:
  ```cpp
  for (int i = 0; i < 5; ++i) 
      cout << arr[i] << " ";
  ```

 **3. Multidimensional Arrays**
- Stored in row-major order (contiguous rows).
  ```cpp
  int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}};
  for (int i = 0; i < 2; ++i) {
      for (int j = 0; j < 3; ++j)
          cout << matrix[i][j] << " ";
  }
  ```

**4. Passing Arrays to Functions**
- Arrays decay to pointers, losing size information. Pass size explicitly:
  ```cpp
  void printArray(int arr[], int size) { ... }
  ```
- For fixed-size arrays, use references:
  ```cpp
  template<size_t N>
  void printArray(int (&arr)[N]) { ... } // N is deduced
  ```

 **5. Limitations and Best Practices**
- **Fixed Size**: Static arrays cannot resize. Prefer `std::vector` for dynamic needs.
- **Bounds Checking**: Not enforced; out-of-bounds access causes undefined behavior.
- **Assignment/Copying**: Use `std::copy` or `memcpy` for raw arrays. `std::array`/`std::vector` support direct assignment.
- **Memory Management**: Dynamic arrays require manual `delete[]`. Prefer RAII containers like `std::vector`.

 **6. STL Alternatives**
- **`std::array`**: Fixed-size, safer wrapper for static arrays.
  ```cpp
  #include <array>
  std::array<int, 5> arr = {1, 2, 3, 4, 5};
  ```
- **`std::vector`**: Dynamic resizing, automatic memory management.
  ```cpp
  #include <vector>
  std::vector<int> vec = {1, 2, 3};
  ```

 **7. Common Pitfalls**
- **Buffer Overflows**: Accessing beyond array bounds.
- **Memory Leaks**: Forgetting `delete[]` for dynamic arrays.
- **Pointer Decay**: Losing size information when passing to functions.

 **Examples**
- **Static Array**:
  ```cpp
  int main() {
      int arr[5] = {1, 2, 3, 4, 5};
      for (int num : arr) // Range-based loop (C++11)
          cout << num << " ";
  }
  ```

- **Dynamic Array**:
  ```cpp
  int main() {
      int size = 5;
      int* arr = new int[size]{1, 2, 3}; // Partial initialization
      delete[] arr;
  }
  ```

 **Conclusion**
Use raw arrays for low-level control or interfacing with C code. Prefer `std::array` (fixed-size) or `std::vector` (dynamic) in modern C++ for safety, flexibility, and automatic resource management.

Comments

Popular posts from this blog

Kotlin Math Operations and Functions Overview

Wear OS Android UI Application in Kotlin

Kotlin Android Program (QCR) Application Codes That Read Text in Photos