Symbian Developer Library

SYMBIAN OS V6.1 EDITION FOR C++

[Index] [Glossary] [Previous] [Next]



How to use new with automatic leave on failure

The case of new failing is so common that a version of operator new() has been written which takes a single parameter ELeave, indicating that it must leave if it was unable to allocate memory.

Use operator new() so that there is no need to check the result of the new.

void doExampleL()
 {
 // attempt to allocate, leave if could not
 CExample* myExample = new (ELeave) CExample;
 // new (ELeave) replaces new followed by check
 // do something
 myExample->iInt = 5;
 testConsole.Printf(_LIT("Value of iInt is %d.\n"),myExample->iInt);
 // delete
 delete myExample;
 }