Tuesday, March 10, 2026

C versus C++

Last week at work, I was helping a colleague with some low level code.  That's because I'm the firmware guy responsible for the bootloader, OS, device drivers, and other low level code.  I wrote a couple functions for him to access capabilities he needed to use.  Since I've been writing C for over 40 years, I write in the classic (or perhaps archaic) style of the first edition of the K&R book .  This involves always returning an integer value (where appropriate) to indicate success or failure and using pointers to return any values which need to be returned.  The next day, he asked if I could help him figure out why it was crashing.  It turns out he didn't understand pointers very well.  To be fair, this engineer is much better at C++ than I am and pointers are a kind of a holdover from the days when C was more like a portable assembly language.  He's also a top notch signal processing engineer.  I ended up fixing his code to access my functions for him because my explanation of pointers didn't click with him.

About 6 months ago I had experienced something similar with another younger engineer.  To be fair, what I call the younger engineers in my group are mostly in their 40s and are all extremely competent.  Anyway, I had written a device driver for him to control the 2 LEDs we have on one of our devices.  Since I come from the days when storage was precious, I had compressed the parameter to control both LEDs and their blink rate (if not solid on or off) into a single 32-bit word.  Doing so involved bit shifting and masking which is second nature to me as I had been trained in simplifying Boolean expressions many years ago.  Engineer #2 didn't understand this code and wanted to reverse it to read what state the LEDs were currently in.  Our company has been pushing an internal version of GPT so he gave it my code and asked for code to reverse my operations to compress all that information into a single word.  It crashed his GPT session and which apparently took several days to clear up.

I think both problems stem from the fact that C++ is heavily object oriented but manages to fool old-timers like me into thinking it's just a fancier version of C.   The problem is being slowly solved as more of the greybeards like me end up retiring which I'm most definitely looking forward to.

No comments:

Post a Comment