Accessing an STL container element with an index past its valid range.
Standard Template Library (STL) methods like `std::vector::at()` or `std::string::at()` perform runtime bounds checking and throw `std::out_of_range` when given an invalid index.
Calling `vec.at(index)` where `index >= vec.size()` or `index < 0`.
1#include <vector>2#include <iostream>3 4int main() {5 std::vector<int> nums = {10, 20, 30};6 std::cout << nums.at(5) << std::endl; // Throws std::out_of_range!7 return 0;8}1#include <vector>2#include <iostream>3 4int main() {5 std::vector<int> nums = {10, 20, 30};6 size_t index = 5;7 8 // Fix 1: Bounds check before accessing9 if (index < nums.size()) {10 std::cout << nums.at(index) << std::endl;11 } else {12 std::cout << "Index out of range" << std::endl;13 }14 15 // Fix 2: Catch exception gracefully16 try {17 std::cout << nums.at(index) << std::endl;18 } catch (const std::out_of_range& e) {19 std::cerr << "Caught exception: " << e.what() << std::endl;20 }21 return 0;22}Simulate standard system builds to trigger compiler trace records and track memory crashes locally.
`nums` has 3 elements (indices 0..2). Calling `nums.at(5)` exceeds `nums.size()`, throwing `std::out_of_range`.