BUG: Array-Style New Allocates Wrong Number of ObjectsLast reviewed: July 22, 1997Article ID: Q113428 |
1.00 1.50
WINDOWS
kbtool kbbuglist
The information in this article applies to:
SYMPTOMSUnexpected results may occur due to errors in the code generated by using any one of the following optimizations: /O2, /O1, /Og, /Oe, /Ol, or /Ox. This problem may be reproduced by using the C++ "new" operator to allocate memory for an array of objects, assuming that the following criteria are also satisfied:
CAUSEThe optimizing compiler is generating incorrect code. This may be seen by creating a .COD listing file by adding /Fc to the compiler options. Depending on the optimization used, the code generated to call "new" will not be passed the correct value.
RESOLUTIONAny of the following will correct this condition for the specific example provided below:
STATUSMicrosoft has confirmed this to be a problem in the Microsoft products listed at the beginning of this article. We are researching this problem and will post new information here in the Microsoft Knowledge Base as it becomes available.
MORE INFORMATION
Sample Code
/* Compile options needed: /f- and /Ox Actually, any one of /O2, /O1, /Og, /Oe, /Ol, or /Ox will cause the problem. */ //--------------------------------------------------------- // Sample to demonstrate code generation problem //--------------------------------------------------------- #include <iostream.h>class AnyName { public: AnyName() { nTimesCalled++; }; static int nTimesCalled;};
int AnyName::nTimesCalled = 0; //--------------------------------------------------------- // main() calls 'new' to demonstrate the problem //--------------------------------------------------------- void main(){ int n = 20; // Indicates the number of // objects to construct AnyName *pAnyName = new AnyName [n]; cout << "Results: " << endl << " Constructor was called " << AnyName::nTimesCalled << " times." << endl << " Constructor should have been called " << (n) << " times." << endl; if ( AnyName::nTimesCalled == (n) ) cout << " Constructor called correct number of times!" << endl << endl; else cout << " Constructor called incorrect number of times!" << endl << endl; } |
Additional reference words: 1.00 1.50 8.00 8.00c
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |