Week 23 contribution of SDK documentation content. See release notes for details. Fixes bugs Bug 2714, Bug 462.
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies) All rights reserved. -->
<!-- This component and the accompanying materials are made available under the terms of the License
"Eclipse Public License v1.0" which accompanies this distribution,
and is available at the URL "http://www.eclipse.org/legal/epl-v10.html". -->
<!-- Initial Contributors:
Nokia Corporation - initial contribution.
Contributors:
-->
<!DOCTYPE concept
PUBLIC "-//OASIS//DTD DITA Concept//EN" "concept.dtd">
<concept id="GUID-7E967B81-2DBF-5A89-B74B-93FEA5515685" xml:lang="en"><title>How
to use new with automatic leave on failure</title><shortdesc>Provides code snippets to show you how to use <codeph>new</codeph> with
automatic leave on failure</shortdesc><prolog><metadata><keywords/></metadata></prolog><conbody>
<p>The case of <codeph>new</codeph> failing is so common that a version of <codeph>operator
new()</codeph> has been written which takes a single parameter <codeph>ELeave</codeph>,
indicating that it must leave if it was unable to allocate memory.</p>
<p>Use <codeph>operator new()</codeph> so that there is no need to check the
result of the <codeph>new</codeph>.</p>
<codeblock id="GUID-E898C802-28E7-5330-B4F1-08829BECB4AE" xml:space="preserve">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;
}</codeblock>
</conbody></concept>