LKBEN10904: Run-Time Check Failure #2 - Stack around the variable x was corrupted.

This article has not been checked!

LKB | Created: 02/04/2020 | Version: 0 | Language: EN | Rating: 0 | Outdated: False | Marked for deletion: False

Author: Wim Peeters - Keskon GmbH & Co. KG


When this happens in a DLL you might get something like External Exception 0x80000003




In C/C++ when a variable is defined in a function, it will go on the stack. When you use new, it will go on the heap. Now consider the following:

char buffer[10];
buffer[11] = "!"; //event buffer[10] is already out of range!

This means you will write over the boundaries of your character array (a string). When you leave this function, you leave the function-scope and the stack around this buffer is corrupted. When you have this kind of error, you should check the variable for writing behind its boundaries. Most of the time you can correct these problems with adding 1 to your array. (This is due to the syntax of C/C++ which makes it difficult to exactly stay in your boundaries)

Please note the following:

char buffer[1]; //this can only contain 1 character! NOT 2!

This means in fact that buffer[1] is already out of range!

The following function will deliver the error:

void testf()
    char buffer[10];

    strcp(buffer, "This is longer than 10 and so will corrupt your stack");

Here I added a whole application and documented where it goes wrong! It is a Qt console application.

#include <QtCore/QCoreApplication>
#include <QTimer>
#include <iostream>

void testf()
        char buffer[1];
        buffer[0] = 'a';
        buffer[1] = 'b'; //here it goes wrong! This is out of the boundaries!

int main(int argc, char *argv[])
    using namespace std;

    QCoreApplication a(argc, argv);

    cout << "Buffertest starts..." << endl;
    cout << "Buffertest ended." << endl;

    QTimer::singleShot(500, &a, SLOT(quit())); //otherwise the program will not end!
    return a.exec();

About the Author

Wim Peeters is electronics engineer with an additional master in IT and over 30 years of experience including time spent in support, development, consulting, training and database administration. Wim has worked with SQL Server since version 6.5. He has developed in C/C++, Java and C# on Windows and Linux in different European countries and different European languages. He writes knowledge base articles to solve IT problems and publishes them on the Lubby Knowledge Platform where he is one of the most important contributors and the main developer.


The information provided in this document is intended for your information only. Lubby makes no claims to the validity of this information. Use of this information is at own risk!