Microsoft KB Archive/318669

From BetaArchive Wiki
Knowledge Base


BUG: Local Variables Are Overwritten When You Use /ZI and /Od Compiler Switches

Article ID: 318669

Article Last Modified on 7/23/2002



APPLIES TO

  • Microsoft Visual C++ .NET 2002 Standard Edition



This article was previously published under Q318669

SYMPTOMS

When you use the /ZI (Program Database for Edit & Continue) and /Od (Disable Optimizations) compiler switches, local variables may be overwritten.

RESOLUTION

To work around this problem, use one of the following methods:

  • Use the /Zi (Program Database) switch instead of the /ZI (Program Database for Edit & Continue) switch.
  • Use the /RTC1 (Basic Runtime Checks) switch, which is equivalent to the /RTCsu switch.


STATUS

Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.

MORE INFORMATION

Although space for all of the local variables is allocated on the stack, some variables may be assigned an address in a local array instead of in the space that is already allocated on the stack. Therefore, if you fill the array, one or more of the local variables may be overwritten even though the return address for the function remains intact.

The following sample code demonstrates the problem:

#include <iostream>
#include <string>

using namespace std;

int main()
{
    const std::string& test = "Some string"; // Creates a temporary string.
    
    cout << "'" << test << "'" << endl;

    {
        char buf[100] = { 0 }; // ERROR: Overwrites the temporary string.
        cout << "'" << test << "'" << endl;
    }
    
    return 0;
}
                

Use the /Od, the EHsc, and the ZI compiler switches to compile this sample.


Additional query words: reference variable

Keywords: kbbug kbfix kbcodegen kbcompiler KB318669