Iterating Backwards in C
Summary
The author explains the common mistakes when iterating backwards in C and presents the clean idiom `for(size_t i = count; i--; )`, which avoids signed-index and wrap-around pitfalls, especially as LLM-generated code repeats the buggy versions.
View Cached Full Text
Cached at: 10/30/26, 03:07 AM
Similar Articles
Going Backward
This article explores the design evolution of Go's slices.Backward function, illustrating different implementations from simple reversal to callback-based iterators, and explains why the standard library's iterator signature looks the way it does.
Getting silly with C, part &((int*)-8)[3]
A humorous educational article covering C programming fundamentals such as forward declarations, operator precedence, unconditional jumps, and basic arithmetic with intentionally silly code examples.
Rotation revisited: A shocking discovery about gcc’s unidirectional rotation algorithm
Raymond Chen explores gcc libstdc++'s rotation algorithm for random-access iterators, revealing it is fundamentally the same as the forward-iterator rotation algorithm, just viewed from a different perspective. The post is part of a series comparing rotation implementations across compilers.
The C ``Clockwise/Spiral Rule''
A classic technique for parsing complex C declarations by following a clockwise/spiral rule, with worked examples.
Neoclassical C++: segmented iterators revisited
Revisits Matt Austern's 2000 paper on segmented iterators, which enable hierarchical algorithms to exploit data structure segmentation for performance, and discusses modern adoption in libc++ and Boost libraries.