A count is not the last valid index

An array containing five elements commonly has valid indices 0 through 4. Treating the count 5 as the final position permits an extra access. Whether this crashes immediately depends on the surrounding memory; it may instead change another value and fail later. Being able to read an address in the debugger does not prove that the address belongs to the logical array. Distinguish the process memory map from the range the program is entitled to use for this object.

Start with empty and one-element inputs

Test lengths 0 and 1 before moving to the failure boundary. Subtracting 1 from an empty length can produce a very large value for an unsigned type. Once the smallest failing size is known, inspect loop initialization, comparison, and increment around the final iteration. DBG Desk has no conditional-breakpoint editor yet, so small inputs also keep repeated stops manageable. Write down the expected iteration count before observing the run; otherwise it is easy to adjust expectations to match the faulty code.

Do not mix characters and bytes

A C string can require room for a zero terminator in addition to its data. A buffer allocated for exactly the data length can overflow when the terminator is written. Text with multibyte characters also has different displayed-character and byte counts. Another common mistake is applying sizeof after an array parameter has become a pointer, expecting the original array capacity. Check where a length is calculated and the actual type in that scope. The source declaration and access instruction should support the same interpretation.

Verify both sides of the boundary

Changing <= to < is not a universal fix. The caller and callee must agree whether an argument means a count, an inclusive end, or an exclusive end. If they disagree, a changed loop can skip data instead of overrunning it. Test the empty case, one element, ordinary sizes, and the documented maximum after the correction. A memory checker can help identify the first invalid access, but the range contract still needs to be clear.

Count a short loop by hand

For a length of 3, record the expected indices 0, 1, and 2. If execution also reaches 3, inspect what allowed the extra iteration. If indexing deliberately starts at 1, confirm whether skipping the first element is intentional. Use the data contract as the reference and make that contract visible in parameter names or documentation.