PRB: Operator New Can't Be Called for CONST or VOLATILE Types (120878)
The information in this article applies to:
- Microsoft Visual C++ 1.0
- Microsoft Visual C++ 1.5
- Microsoft Visual C++ 1.51
- Microsoft Visual C++ 1.52
- Microsoft Visual C++ 2.0
- Microsoft Visual C++ 2.1
- Microsoft Visual C++ 4.0
- Microsoft Visual C++, 32-bit Enterprise Edition 5.0
- Microsoft Visual C++, 32-bit Professional Edition 5.0
This article was previously published under Q120878 SYMPTOMS
When trying to allocate memory from the heap on const or volatile
types, you may see this error:
error C2468: 'new' : cannot allocate 'const'/'volatile' objects
(type is 'const int ')
CAUSE
This behavior is by design. For any type T, the type-specifier
cannot contain const T or volatile T. For example, the following
type-specifiers won't work:
new const int; // Returns 'const int *'
new volatile int; // Returns 'volatile int *'
new int * const; // Returns 'int * const *'
new int * volatile; // Returns 'int * volatile *'
RESOLUTION
The following type-specifiers will work:
new int const *; // Returns 'int const **' (or 'const int**')
new int volatile *; // Returns 'int volatile **'
// (or 'volatile int**')
Modification Type: | Major | Last Reviewed: | 12/2/2003 |
---|
Keywords: | kberrmsg kbLangCPP kbprb KB120878 |
---|
|