Category: C/C++ Trips & Tips
-
Build Your Own std::vector in C
Over the years of working with C, I often found myself stuck when dealing with dynamically resizable data structures. More often than not, I’d end up allocating a huge fixed-size array which worked at first but soon became a pain to maintain. Tracking index positions, avoiding segmentation faults, and ensuring I wasn’t accessing non-existent memory…
-
Why 0.3 Is Not Exactly 0.3: Understanding Floating-Point Precision in Programming
If you’ve ever written code like this: …and wondered why the “obvious” answer fails? Welcome to the fascinating (and sometimes infuriating) world of floating-point numbers. The Invisible Problem At first glance, 0.1 + 0.2 should be 0.3. Simple, right? But computers don’t store decimal numbers in base 10—they store them in binary, as a sequence…