Symbian3/SDK/Source/GUID-02E674F7-86F1-5A9C-8189-F4CEE80D9FBD.dita
changeset 0 89d6a7a84779
equal deleted inserted replaced
-1:000000000000 0:89d6a7a84779
       
     1 <?xml version="1.0" encoding="utf-8"?>
       
     2 <!-- Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies) All rights reserved. -->
       
     3 <!-- This component and the accompanying materials are made available under the terms of the License 
       
     4 "Eclipse Public License v1.0" which accompanies this distribution, 
       
     5 and is available at the URL "http://www.eclipse.org/legal/epl-v10.html". -->
       
     6 <!-- Initial Contributors:
       
     7     Nokia Corporation - initial contribution.
       
     8 Contributors: 
       
     9 -->
       
    10 <!DOCTYPE concept
       
    11   PUBLIC "-//OASIS//DTD DITA Concept//EN" "concept.dtd">
       
    12 <concept id="GUID-02E674F7-86F1-5A9C-8189-F4CEE80D9FBD" xml:lang="en"><title>How
       
    13 to remove elements</title><shortdesc>It is possible to remove one or several contiguous elements from
       
    14 any array. Deleting elements from an array may not automatically free up memory.</shortdesc><prolog><metadata><keywords/></metadata></prolog><conbody>
       
    15 <p>Elements can be removed from an array.</p>
       
    16 <p>The following code fragment shows the deletion of the second <codeph>TElement</codeph> object
       
    17 from a <codeph>CArrayFixFlat&lt;class T&gt;</codeph> array:</p>
       
    18 <codeblock id="GUID-B76A9F01-85DA-5BE3-A89E-67AEA5011A5F" xml:space="preserve">class TElement
       
    19  {
       
    20 public :
       
    21  TElement();
       
    22 public :
       
    23  TBuf&lt;4&gt; iData;
       
    24  };</codeblock>
       
    25 <codeblock id="GUID-97D12B24-E03C-549F-975D-E2DD72A11A81" xml:space="preserve">CArrayFixFlat&lt;TElement&gt;* fixflat;
       
    26 fixflat = new (ELeave) CArrayFixFlat&lt;TElement&gt;(3);
       
    27 ...
       
    28 ...        // elements added to the array at some point
       
    29 ...
       
    30 fixflat-&gt;Delete(1);</codeblock>
       
    31 <p>To delete what are now the third and fourth elements from the array, i.e.
       
    32 the two contiguous elements starting at position two, use <codeph>Delete()</codeph> and
       
    33 specify both the starting position and the number of elements:</p>
       
    34 <codeblock id="GUID-041A95DA-A332-5CBE-8D28-D0257DCC5255" xml:space="preserve">fixflat-&gt;Delete(2,2);</codeblock>
       
    35 <section id="GUID-B9040865-C163-4A0B-B509-DAFA8BC39CE2"><title>Notes</title> <ul>
       
    36 <li id="GUID-AFA7DA8D-26A5-5FCA-A4F8-25627D209CE8"><p>Deleting elements from
       
    37 an array does not automatically compress the array buffer; the space allocated
       
    38 to the array buffer remains unchanged. This means that adding an element to
       
    39 a <i>same length</i> type array <i>after</i> deleting an element is <i>guaranteed</i> not
       
    40 to fail. However, this is <i>not</i> true for a variable length array and
       
    41 is only true for a packed array if the size of the new element is less than
       
    42 or equal to the size of the deleted element.</p> </li>
       
    43 <li id="GUID-3DE5FD87-8557-586C-84E7-7F2105483296"><p>Excess space in the
       
    44 array buffer can be removed using the <codeph>Compress()</codeph> member function.
       
    45 This reduces the capacity of the array. For a same length flat array, an attempt
       
    46 to add an element after compression causes re-allocation of the flat buffer.
       
    47 To compress the array:</p> </li>
       
    48 </ul> <codeblock id="GUID-A9C18AD8-1D3B-5F2A-80A6-FD06846C1D0C" xml:space="preserve">fixflat-&gt;Compress();</codeblock> <ul>
       
    49 <li id="GUID-7595C77D-73A7-539C-B18B-2E1DE51EF4BA"><p>All elements within
       
    50 an array can be deleted using the <codeph>Reset()</codeph> function. This
       
    51 function also compresses the array. Compressing the array when it is empty
       
    52 causes the memory allocated to the array buffer to be freed.</p> </li>
       
    53 <li id="GUID-00AADD5C-E4E0-5C68-B1D3-4906973A6299"><p>For an array of pointers
       
    54 to <codeph>CBase</codeph> derived objects (i.e. a <codeph>CArrayPtrFlat</codeph> or
       
    55 a <codeph>CArrayPtrSeg</codeph> type array), some care should be taken when
       
    56 deleting elements from the array. The following code fragment deletes the
       
    57 last two elements from <codeph>ptrflat</codeph> (a <codeph>CArrayPtrFlat</codeph> array)
       
    58 and deletes the objects that those elements point to:</p> </li>
       
    59 </ul> <codeblock id="GUID-B853D5EE-11A0-50C7-9CC6-6705A5118AC3" xml:space="preserve">TInt      index;
       
    60 CElement* ptr;
       
    61 ...
       
    62 index = ptrflat-&gt;Count();
       
    63 ...
       
    64 ptr = (*ptrflat)[--index];
       
    65 ptrflat-&gt;Delete(index);
       
    66 delete ptr;
       
    67 ...
       
    68 ptr = ptrflat-&gt;At(--index);
       
    69 ptrflat-&gt;Delete(index);
       
    70 delete ptr;</codeblock> <ul>
       
    71 <li id="GUID-0C733A40-7B7D-518B-B27F-5E25DB73435A"><p>In general, before the
       
    72 elements of this kind of array are deleted, the <codeph>CBase</codeph> derived
       
    73 objects to which those elements point should be destroyed. If they are not
       
    74 destroyed, then a separate copy of those elements (i.e. the pointers), <i>must</i> be
       
    75 taken to avoid orphaning the <codeph>CBase</codeph> derived objects on the
       
    76 heap.</p> </li>
       
    77 </ul> </section>
       
    78 </conbody></concept>