Article ID: 156808
Article Last Modified on 7/13/2005
<new>
void *operator new(size_t n) void *operator new(size_t n, const nothrow&) void *operator new[](size_t n);Note The class/parameter names in the prototype may not match the version in the header file. Some have been modified to improve readability.
//////////////////////////////////////////////////////////////////////
//
// Compile options needed: /GX
//
// <filename> : newop.cpp
//
// Functions:
//
// void *operator new(size_t n)
//
// void *operator new(size_t n, const nothrow&)
//
// void *operator new[](size_t n);
//
// Written by Linda Koontz
// of Microsoft Product Support Services,
// Copyright (c) 1996 Microsoft Corporation. All rights reserved.
//////////////////////////////////////////////////////////////////////
/* Compile options needed: /GX
*/
#include <new>
#include <iostream>
#if _MSC_VER > 1020 // if VC++ version is > 4.2
using namespace std; // std c++ libs implemented in std
#endif
class BigClass {
public:
BigClass() {};
~BigClass(){}
double BigArray[99999999];
};
void main()
{
try {
BigClass * p = new BigClass;
}
catch( bad_alloc a) {
const char * temp = a.what();
cout << temp << endl;
cout << "Threw a bad_alloc exception" << endl;
}
BigClass * q = new(nothrow) BigClass;
if ( q == NULL )
cout << "Returned a NULL pointer" << endl;
try {
BigClass * r = new BigClass[3];
}
catch( bad_alloc a) {
const char * temp = a.what();
cout << temp << endl;
cout << "Threw a bad_alloc exception" << endl;
}
}
void *operator new(size_t n) throw(bad_alloc);
void *operator new(size_t n, const nothrow&) throw();
void *operator new[](size_t n) throw(bad_alloc);
The throw() following the prototype is an exception specification that
is not implemented in Visual C++ version 4.2. This is noted in the Online Help:
Microsoft C++ does not support the function exception specification mechanism, as described in section 15.4 of the ANSI C++ draft.An exception-specification specifies the type of exceptions a function can throw. For example,
void Func() throw (ProblemOne, ProblemTwo) {}
is equivalent to:
void Func() {
{
try {}
catch (ProblemOne) {}
catch (ProblemTwo) {}
catch (...) { unexpected(); }
}
Additional query words: STL STLSample new
Keywords: kbhowto kbinfo kbcode KB156808