Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.

Tip of the Day
Language: C++
Expertise: Intermediate
Jul 9, 1999



Building the Right Environment to Support AI, Machine Learning and Deep Learning

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;
  int arr[2]; //original array
#ifdef DEBUG
  int after_sentry = -99;

  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");

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.

Danny Kalev
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date