×

Discussion Board

Results 1 to 11 of 11
  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    339

    how to rearrange index of RPointerArray?

    hi, I need to do random addition and deletion from array and will store pointers in the array. Now I am using RPointerArray for the purpose. Is there any method to re-arrange array index after deletion an item from the array. let's say I have 4 items in the array ( 0,1,2,3) now item 2 is deleted from the array ( 0,1,3). Is there any method to rearrange array index to (0,1,2) .Or is there any other data structure suitable for the purpose? Thanks

  2. #2
    Nokia Developer Champion
    Join Date
    Jun 2008
    Location
    Noida,India
    Posts
    3,988

    Re: how to rearrange index of RPointerArray?

    I think the array re-organizes itself when an element is removed from it(RPointerArray's ,Remove() method does that i suppose).

  3. #3
    Nokia Developer Moderator
    Join Date
    Mar 2003
    Location
    Lempäälä/Finland
    Posts
    29,167

    Re: how to rearrange index of RPointerArray?

    it does indeed reduce the indexes after deletion, so if you have experienced problems with it, you might want to share the code sample which is not working as expected.

  4. #4
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    28,674

    Re: how to rearrange index of RPointerArray?

    (Just a minor remark: you still need the delete for the pointed object, RPointerArray::Remove removes the pointer only
    Code:
    delete iArray[i];
    iArray.Remove(i);
    )

  5. #5
    Registered User
    Join Date
    Dec 2009
    Posts
    339

    Re: how to rearrange index of RPointerArray?

    Thanks vineet.jain and symbianyucca for reply. My code looks as follows


    Code:
    RPointerArray<CMyObject> iPtrArr;
    CMyObject* iMyObj;
    
    void RemoveObject(TInt index)
    {
    	CMyObject* obj = iPtrArr.operator [](index);
    	iPtrArr.Remove(index);
    	delete obj;
    	obj = NULL;
    
    	iPtrArr.Compress();
    		
    }
    
    
    
    void GetObject(TInt index)
    {	
    	
    	if(index<iPtrArr.Count)
    		CMyObject* obj = iPtrArr.operator [](index);
    	if(obj)
    	{
    	   iMyObj = obj;
    	}
    }

    I am sometimes getting KERN-EXEC 3 while using above codes.I have to randomly add and remove objects from the array.It may happen that array has 4 items ( 0,1,2,3 ), now item 1 is deleted and then GetObject is called with index 1. what will happen in that case?
    thanks

  6. #6
    Nokia Developer Moderator
    Join Date
    Mar 2003
    Location
    Lempäälä/Finland
    Posts
    29,167

    Re: how to rearrange index of RPointerArray?

    you might also add if(index<iPtrArr.Count && index >=0), so you would not get -1 as an index.

    Basically if you delete index 1, then your original index 2 will be at index 1

  7. #7
    Registered User
    Join Date
    Dec 2009
    Posts
    339

    Re: how to rearrange index of RPointerArray?

    Quote Originally Posted by wizard_hu_ View Post
    (Just a minor remark: you still need the delete for the pointed object, RPointerArray::Remove removes the pointer only
    Code:
    delete iArray[i];
    iArray.Remove(i);
    )
    Thanks wizard_hu_. Will it not do the same task?

    Code:
    CMyObject* obj = iPtrArr.operator [](index);
    iPtrArr.Remove(index);
    delete obj;
    obj = NULL;

  8. #8
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    28,674

    Re: how to rearrange index of RPointerArray?

    Effectively yes. However I have to say I like my coding style more. 2 lines vs. 4 lines, no dummy variables involved. Also, operators are exactly invented for making code more readable. iPtrArr.operator[](index) vs. iPtrArr[index]...

  9. #9
    Registered User
    Join Date
    Dec 2009
    Posts
    339

    Re: how to rearrange index of RPointerArray?

    Quote Originally Posted by symbianyucca View Post
    Basically if you delete index 1, then your original index 2 will be at index 1
    Thanks symbianyucca. That is what I need to know, need to figure out why getting KERN-EXC 3 on some cases.

  10. #10
    Nokia Developer Moderator
    Join Date
    Mar 2003
    Location
    Lempäälä/Finland
    Posts
    29,167

    Re: how to rearrange index of RPointerArray?

    Quote Originally Posted by slinx View Post
    Thanks symbianyucca. That is what I need to know, need to figure out why getting KERN-EXC 3 on some cases.
    if you first try that one, and then you could also check that you are not adding invalid pointers to the array in the beginning..

  11. #11
    Registered User
    Join Date
    May 2011
    Posts
    35

    Re: how to rearrange index of RPointerArray?

    -The null dereference could be happening in the following sequence:

    1) GetObject(indexX);
    2) RemoveObject(indexX);
    3) Then you access iMyObj

    Probably worth checking iMyObj against the pointer you delete in RemoveObject() and setting it to null if they are equal.

    - Alternatively GetObject() looks a little iffy. Heap pointers are not null initialised, if the index passed in is out of bounds what is "obj" initialised to in your current code?

Similar Threads

  1. Replies: 7
    Last Post: 2012-08-30, 17:40
  2. RPointerArray:
    By maheskumar in forum Symbian Tools & SDKs
    Replies: 2
    Last Post: 2011-01-03, 13:51
  3. Replies: 3
    Last Post: 2008-04-23, 18:24
  4. RPointerArray
    By Deepa msc in forum General Development Questions
    Replies: 5
    Last Post: 2008-01-16, 09:43

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×