An ounce of prevention is worth a pound of cure. Take either addend and subtract it from the maximum. If the result is smaller than the other addend, you will overflow. If Max - A < B, then A + B > Max // OVERFLOW
Lurk
Posts
-
C++ integer arithmetic overflow -
Progressives.... not getting along with meSounds like me some years ago or maybe decades. bifocals drove me up the wall. Trifocals were worse. I finally gave up and went with contacts. They have bifocals, too. Seemed to be more of an average between near and far. Great compromise. Always out of focus. Now I do contacts with mono-vision. One eye for near and one for far. It is surprising. Your eyes just do it for you. My prescription is very light, though. Maybe that makes a difference. I did bounce back and forth between contacts and glasses a few times along the way. Both were frustrating. Mono-vision is what finally worked.
-
OO Software design epiphany - it might not matterThe real problem with OO Programming and possibly Design is that everyone knows the parts, but most don't put them together well. OO has worked well for years and it is visible at the low level. Consider the storage device. It can be a floppy (remember those?), a hard drive, an SSD, a write once, CD/DVD, an online storage, an so many more. But the block storage protocol is applied to all. the Driver converts the hardware to an object that responds to the same inputs no matter what is hidden behind the interface. The device is the object. The functionality is abstracted and hidden. The behavior is locked behind the wall. And the new types of devices are added invisibly to the higher levels of code by following the interface rules. Enhancements to the rules are added all the time by extending the interface for things that were not previously considered. The trick is in creating the specific, but only working with the generic. The failure is in creating specific necessary behavior at the derived level that cannot be used at the generic level. Hierarchy of Animal -> Quadruped -> Horse. Horse whinnies and gallops. But to implement them as Horse features means that they must be addressed as features of a horse. Instead, Animal Speaks(Friendly | Loudly | Fearfully ), Moves( Slowly | Quickly ) Now, we can say Animal->Speak(Friendly), Animal->Moves( Quickly). We can add Dog and a Cat and implement the interface described. Then we create the item and put it in Animal and use it without changing the code at all. The biggest trick of all, is to construct the Animal (or derived type) with as much of the descriptive information as possible. then use it with as little information as possible. Compare to the original Storage idea. the File Create and Open takes all kinds of descriptive information, but the actions on it take only the necessary variations. File->Read( howMuch, toWhere). File->Seek(toPosition, fromWhere). It isn't really the tenants of OO that are in question, it is the organization. Put them together and use them well and they provide for an easier path to expansion, adaptation and improvements. Print is a great example of Polymorphism. Print (integer) Print (string) Print (float) Print (format, arg1, arg2, arg3, ...) Print (Animal) OO is more than a Hammer. It is a whole Toolbox than can be used to build better tools and expandable structures.