Using “Sentry Bytes” to Detect Memory Overruns

One of the most frustrating aspects of debugging is detecting memory overruns. Some compilers (MS Visual C++ for instance) automatically add “sentry bytes” at the ends of every memory block allocated by operator new. The sentry bytes have a predefined value that the runtime system monitors. When the predefined value changes, this indicates that the program wrote to an out of bound memory address. Unfortunately, compilers apply this technique to heap memory exclusively. To detect memory overruns in stack memory, you can implement this technique manually. This code snippet writes one position beyond the last element of the array:

 void erratic(){  int arr[2];  for (int j= 0; j<3; j++) //this loop iterates one to many times      arr[j] = j;}

In its last iteration, the loop writes the value 2 to a memory address that is beyond the valid array bounds. This version of the previous code snippet uses two initialized sentry integers that are declared immediately before and after the original array. The values of the sentry integers are then examined to detect the memory overrun:

 void erratic(){#ifdef DEBUG  int before_sentry = -99;#endif  int arr[2]; //original array#ifdef DEBUG  int after_sentry = -99;#endif  for (int j= 0; j<3; j++)        arr[j] = j;#ifdef DEBUG  if (before_sentry != -99 )     throw X("memory overrun detected at right before arr, at function erratic");  if (after_sentry != -99 )  //true      throw X("memory overrun detected at right after arr, at function erratic");#endif}

Instead of writing the extra test code manually, you can use a clever macro that does this job for you. Note that this technique does not incur any runtime overhead in the release version.

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


The Latest

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS

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