Undefined Behavior vs. Unspecified Behavior

The terms “undefined behavior” and “unspecified behavior” are not interchangeable. Undefined behavior indicates that an implementation may behave unpredictably when a program reaches a certain state, which almost without exception is a result of a bug. Undefined behavior can be manifested as a run time crash, unstable and unreliable program state, or–in rare cases–it may even pass unnoticed. Examples of undefined behaviors include an attempt to write to a buffer past its boundary, accessing an out of range array subscript, de-referencing a dangling pointer or deleting a non-NULL pointer more than once.

Unspecified behavior, on the other hand, is a consistent and documented behavior that a certain implementation employs in cases that are left intentionally unspecified by the C++ Standard–usually, to allow efficient and simple compiler implementation on various platforms. Examples for unspecified behaviors include the size of an int, whether a char is unsigned or signed by default, the size of a pointer, or the underlying type representing a bool. It is guaranteed that the behavior of a certain implementation in such cases is consistent. For instance, all variables of type int are guaranteed to have the same size on the same machine. It is not guaranteed, however, that on another machine, int variables will occupy the same size.

To sum up, unspecified behavior is usually something you shouldn’t worry about, unless your software is required to be portable. Conversely, undefined behavior is always undesirable and should never occur.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

homes in the real estate industry

Exploring the Latest Tech Trends Impacting the Real Estate Industry

The real estate industry is changing thanks to the newest technological advancements. These new developments — from blockchain and AI to virtual reality and 3D printing — are poised to change how we buy and sell homes. Real estate brokers, buyers, sellers, wholesale real estate professionals, fix and flippers, and beyond may

man on floor with data

DevX Quick Guide to Data Ingestion

One of the biggest trends of the 21st century is the massive surge in internet usage. With major innovations such as smart technology, social media, and online shopping sites, the internet has become an essential part of everyday life for a large portion of the population. Due to this internet

payment via phone

7 Ways Technology Has Changed Traditional Payments

In today’s digital world, technology has changed how we make payments. From contactless cards to mobile wallets, it’s now easier to pay for goods and services without carrying cash or using a checkbook. This article will look at seven of the most significant ways technology has transformed traditional payment methods.