Symbian3/SDK/Source/GUID-7E967B81-2DBF-5A89-B74B-93FEA5515685.dita
author Graeme Price <GRAEME.PRICE@NOKIA.COM>
Fri, 15 Oct 2010 14:32:18 +0100
changeset 15 307f4279f433
parent 0 89d6a7a84779
permissions -rw-r--r--
Initial contribution of the Adaptation Documentation.

<?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-&gt;iInt = 5;
 testConsole.Printf(_LIT("Value of iInt is %d.\n"),myExample-&gt;iInt);
 // delete
 delete myExample;
 }</codeblock>
</conbody></concept>