Symbian3/SDK/Source/GUID-02E674F7-86F1-5A9C-8189-F4CEE80D9FBD.dita
author Dominic Pinkman <dominic.pinkman@nokia.com>
Tue, 20 Jul 2010 12:00:49 +0100
changeset 13 48780e181b38
parent 0 89d6a7a84779
permissions -rw-r--r--
Week 28 contribution of SDK documentation content. See release notes for details. Fixes bugs Bug 1897 and Bug 1522.

<?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-02E674F7-86F1-5A9C-8189-F4CEE80D9FBD" xml:lang="en"><title>How
to remove elements</title><shortdesc>It is possible to remove one or several contiguous elements from
any array. Deleting elements from an array may not automatically free up memory.</shortdesc><prolog><metadata><keywords/></metadata></prolog><conbody>
<p>Elements can be removed from an array.</p>
<p>The following code fragment shows the deletion of the second <codeph>TElement</codeph> object
from a <codeph>CArrayFixFlat&lt;class T&gt;</codeph> array:</p>
<codeblock id="GUID-B76A9F01-85DA-5BE3-A89E-67AEA5011A5F" xml:space="preserve">class TElement
 {
public :
 TElement();
public :
 TBuf&lt;4&gt; iData;
 };</codeblock>
<codeblock id="GUID-97D12B24-E03C-549F-975D-E2DD72A11A81" xml:space="preserve">CArrayFixFlat&lt;TElement&gt;* fixflat;
fixflat = new (ELeave) CArrayFixFlat&lt;TElement&gt;(3);
...
...        // elements added to the array at some point
...
fixflat-&gt;Delete(1);</codeblock>
<p>To delete what are now the third and fourth elements from the array, i.e.
the two contiguous elements starting at position two, use <codeph>Delete()</codeph> and
specify both the starting position and the number of elements:</p>
<codeblock id="GUID-041A95DA-A332-5CBE-8D28-D0257DCC5255" xml:space="preserve">fixflat-&gt;Delete(2,2);</codeblock>
<section id="GUID-B9040865-C163-4A0B-B509-DAFA8BC39CE2"><title>Notes</title> <ul>
<li id="GUID-AFA7DA8D-26A5-5FCA-A4F8-25627D209CE8"><p>Deleting elements from
an array does not automatically compress the array buffer; the space allocated
to the array buffer remains unchanged. This means that adding an element to
a <i>same length</i> type array <i>after</i> deleting an element is <i>guaranteed</i> not
to fail. However, this is <i>not</i> true for a variable length array and
is only true for a packed array if the size of the new element is less than
or equal to the size of the deleted element.</p> </li>
<li id="GUID-3DE5FD87-8557-586C-84E7-7F2105483296"><p>Excess space in the
array buffer can be removed using the <codeph>Compress()</codeph> member function.
This reduces the capacity of the array. For a same length flat array, an attempt
to add an element after compression causes re-allocation of the flat buffer.
To compress the array:</p> </li>
</ul> <codeblock id="GUID-A9C18AD8-1D3B-5F2A-80A6-FD06846C1D0C" xml:space="preserve">fixflat-&gt;Compress();</codeblock> <ul>
<li id="GUID-7595C77D-73A7-539C-B18B-2E1DE51EF4BA"><p>All elements within
an array can be deleted using the <codeph>Reset()</codeph> function. This
function also compresses the array. Compressing the array when it is empty
causes the memory allocated to the array buffer to be freed.</p> </li>
<li id="GUID-00AADD5C-E4E0-5C68-B1D3-4906973A6299"><p>For an array of pointers
to <codeph>CBase</codeph> derived objects (i.e. a <codeph>CArrayPtrFlat</codeph> or
a <codeph>CArrayPtrSeg</codeph> type array), some care should be taken when
deleting elements from the array. The following code fragment deletes the
last two elements from <codeph>ptrflat</codeph> (a <codeph>CArrayPtrFlat</codeph> array)
and deletes the objects that those elements point to:</p> </li>
</ul> <codeblock id="GUID-B853D5EE-11A0-50C7-9CC6-6705A5118AC3" xml:space="preserve">TInt      index;
CElement* ptr;
...
index = ptrflat-&gt;Count();
...
ptr = (*ptrflat)[--index];
ptrflat-&gt;Delete(index);
delete ptr;
...
ptr = ptrflat-&gt;At(--index);
ptrflat-&gt;Delete(index);
delete ptr;</codeblock> <ul>
<li id="GUID-0C733A40-7B7D-518B-B27F-5E25DB73435A"><p>In general, before the
elements of this kind of array are deleted, the <codeph>CBase</codeph> derived
objects to which those elements point should be destroyed. If they are not
destroyed, then a separate copy of those elements (i.e. the pointers), <i>must</i> be
taken to avoid orphaning the <codeph>CBase</codeph> derived objects on the
heap.</p> </li>
</ul> </section>
</conbody></concept>