Runtime Error: HEAP CORRUPTION DETECTED: before Normal block


This type of error is generated by writing to an element of the array outside of the allocated range of indices. Typically, writing to an element outside of the index range corrupts the heap.

If you make an array of 3, for example, and then try to write data to the element at the index -1, such as this:

#include <iostream>

int main() {

    double* dpArray = new double[3];
    dpArray[0] = 3.2;
    dpArray[1] = 4.5;
    dpArray[2] = 10.2;

    dpArray[-1] = 11.3;

    delete [] dpArray;


    return 0;

When you run the program in Debug mode (click on the menu Debug -> Start Debugging), you will get this error:

It is usually not clear what this error means. However, you'll find that by assigning data to the -1 index of the array, information about the array which is stored in memory before the 0th index of the array gets changed. Then, when you try to delete the array, the program cannot find the correct information it needs at the array's -1st position in memory. The heap has been corrupted, and the program errors out.

The way to fix this is to make sure in your code that you are never accessing the -1st index of an array. While that sounds obvious, if you set up a for or while loop and decrement your index, if you are not careful you may run past the 0th index of the array and cause this error.