CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    May 2009
    Posts
    88

    Post regarding CTypedPtrArray,moving of array

    Well am designing an application in which i am using CTypedPtrArray for saving an pointers of array now at any instant how can i delete any of the value at given index and then move the whole rest of the array,is their any other suggestion rather than moving of complete array........
    Please help me...

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: regarding CTypedPtrArray,moving of array

    Is there any reason to use CTypedPtrArray rather than CArray<...> of objects?
    Victor Nijegorodov

  3. #3
    Join Date
    May 2009
    Posts
    88

    Re: regarding CTypedPtrArray,moving of array

    yeah actually am not well acquainted with CArray<>...i have used this to save records of class that's derived from CObject....wel actually i solved my other problems the only problem am facing is i have to delete the contents of last index of CTypedPtrArray.......am presenting you the code...
    Code:
    void CPflowView::OnDeleteBus() 
    {
    	// TODO: Add your command handler code here
    	CPflowDoc *pDoc=GetDocument();
    	ASSERT_VALID(pDoc);
    	CDeletebox delb;
    	delb.DoModal ();
    	int index=pDoc->GetNumBus();
    	int x;
    	while(index--)
    	{
    		
    		if(pDoc->GetBus (index)->m_BusNo ==delb.m_no)
    		{	
    			x=0;
    			break;
    		}
    		else 
    			x=1;
    	}
    	if (x==0)
    	{
    		x=pDoc->GetNumBus();
    		for(int i=++index;i<=(x-1);i++)
    		{
    			pDoc->PutBus(pDoc->GetBus(index),(--index));
    		}
    				
    	}
    		
    //want to delete the content of last index here
    
    }
    well here the PutBus function is used to shift the contents of CTypedPtrArray.........

  4. #4
    Join Date
    May 2009
    Posts
    88

    Re: regarding CTypedPtrArray,moving of array

    can somebody tell me what is setatgrow function suppose to do.....
    please help me....

    thnx

  5. #5
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: regarding CTypedPtrArray,moving of array

    It is not really clear what you are trying to do. There is a RemoveAt() member
    function that removes an element in the collection at a specified index. (you still
    need to take care of deallocating dynamicallt allocated memory)

  6. #6
    Join Date
    May 2009
    Posts
    88

    Re: regarding CTypedPtrArray,moving of array

    can you please tell if i use RemoveAt function then how can i reduce the size of of CTypedPtrArray since am not allocating memory for it anywhere specifically.....

  7. #7
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: regarding CTypedPtrArray,moving of array

    MFC will reduce the memory allocated by CTypedPtrArray object after you call RemoveAt. You, however, have to free the memory the pointer at the CTypedPtrArray position, you are removing at, points to (since you created your object with new you must now delete it using delete).

    Besides, in the code snippet you have posted is absolutely unclear what CTypedPtrArray is and what type of objects you are creating (which pointers are supposed to be set at the CTypedPtrArray)
    Victor Nijegorodov

  8. #8
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: regarding CTypedPtrArray,moving of array

    I don't understand. You must be adding elements to the array at some time.

  9. #9
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: regarding CTypedPtrArray,moving of array

    Maybe a simple example will help. I used a message box in the
    example for demonstration purposes only ... you should use
    a debugger to look at the results

    Code:
    struct CValue : public CObject
    {
        CValue(int val): value(val) {}
        int value;
    };
    
    
    // ...
    
    CTypedPtrArray<CPtrArray,CValue*> arr;
    
    // add some initial elements to the colection
    
    arr.Add(new CValue(0)); 
    arr.Add(new CValue(1)); 
    arr.Add(new CValue(2)); 
    arr.Add(new CValue(3)); 
    arr.Add(new CValue(4)); 
    
    // set the value of arr[7] .. if necessary, the size of the collection
    // will increase to 8.  Any "skipped" index positions we be st to NULL
    
    arr.SetAtGrow(7, new CValue(99) );
    
    // free the dynamically allocated memory for the third element
    // and remove it from the collection
    
    delete arr[2];
    arr.RemoveAt(2);
    
    // loop thru the colection 
    for (int i=0; i<arr.GetCount(); ++i)
    {
        if (arr[i] != 0)
        {
            CString s;
            s.Format(_T("&#37;d"),arr[i]->value);
            AfxMessageBox(s);
        }
        else
        {
            AfxMessageBox(_T("null pointer"));
        }
    }
    
    // "messagebox output" = 0 , 1  , 3 , 4 , null pointer , null pointer , 99
    
    // free all dynamically allocated memory and remove all elements from
    // the collection
    
    for (int i=0; i<arr.GetSize(); ++i)
    {
        delete arr[i];
    }
    arr.RemoveAll();

Tags for this Thread

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured