PRB: AllocMemoryDebug Gives Assertion or Trace Error on New[0] (125309)
The information in this article applies to:
- The Microsoft Foundation Classes (MFC), when used with:
- Microsoft Visual C++ 1.51
- Microsoft Visual C++, 32-bit Editions 1.0
This article was previously published under Q125309 SYMPTOMS
If new is called to allocate an array whose size is determined by a
variable that has a value of zero (0), the program terminates with the
following message in a debug build:
Assertion Failed! <name> Windows Application:File Afxmem.cpp,
Line <line>
The <line> value depends on the version of MFC that you have:
- In MFC 2.0 <line> = 275
- In MFC 2.1 <line> = 267
- In MFC 2.5 <line> = 277
- In MFC 2.51 <line> = 286
In MFC 3.x, the code generates a TRACE message similar to this:
Warning: Allocating zero length memory block.
RESOLUTION
This behavior changed with the introduction of Visual C++ version 2.0. In
the MFC that shipped with version 2.0, a TRACE is output to the Debug
Window in Debug mode.
If you encounter the assertion failure, you can work around it by simply
not calling new for a zero-length array. Check for a zero value before
calling new. If you need to have a valid address for the memory block,
allocate an array of size 1 as in new[1].
STATUS
This behavior is by design. It is intended to help programmers detect logic
errors in programs.
Modification Type: | Major | Last Reviewed: | 12/8/2003 |
---|
Keywords: | kbNoUpdate kbprb KB125309 |
---|
|