main.cpp
CONTIGUOUS MEMORY STORAGE:
Index: arr[0] arr[1] arr[2] arr[3]
Offset: +0B +4B +8B +12B (for 4-byte integers)CONTIGUOUS MEMORY STORAGE:
Index: arr[0] arr[1] arr[2] arr[3]
Offset: +0B +4B +8B +12B (for 4-byte integers)int arr[5]; (Raw array: decays to pointer, lacks size method, no bounds checking).std::array arr; (STL wrapper: knows its size .size(), iterable, zero runtime overhead).#include <iostream>
#include <array>
int main() {
// Modern std::array
std::array<int, 5> scores = {92, 85, 78, 95, 88};
scores[0] = 96; // Direct indexing
std::cout << "Array Size: " << scores.size() << "
";
std::cout << "Elements: ";
for (int score : scores) {
std::cout << score << " ";
}
std::cout << "
";
// Safe bounds-checked access
try {
std::cout << "Element at index 2: " << scores.at(2) << "
";
// scores.at(10); // Throws std::out_of_range
} catch (const std::out_of_range& e) {
std::cerr << "Out of range error caught: " << e.what() << "
";
}
return 0;
}