devxlogo

Using “Sentry Bytes” to Detect Memory Overruns

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

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

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.

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist