Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Serializing CArray

Posted on 1999-01-09
13
Medium Priority
?
499 Views
Last Modified: 2013-11-20
Is serializing CArray any different from serializing any other object derived from CObject? I am trying to serialize an CArray of PContact structs. I don't get any errors or warnings, but when I read the contents of one of the elements of the array, it comes out like junk... then crashes the program. I will post code if needed. BTW, struct PContact contains CString objects... would that effect the results because CString uses pointers?
0
Comment
Question by:RMD010799
13 Comments
 

Author Comment

by:RMD010799
ID: 1327417
Edited text of question
0
 
LVL 12

Expert Comment

by:migel
ID: 1327418
Your program crushes due to CArray::serealize store memory dump of the items, so when reading stored CString object you get wrong pointers. Best way to avoid this problem is write own code for imlpement serealization.

0
 

Author Comment

by:RMD010799
ID: 1327419
When you say write my own code for the serialzation... do you mean for serialize CArray or for serializing each of my PContact structs?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 12

Expert Comment

by:migel
ID: 1327420
CArray do not call any serealization functions of the stored objects by default. So you need to serealize size of the array and each stored object manually.

for example:
void SerealizeArray(CArray<PContact, PContact>& arContact, CArchive& ar)
{
      CObject::Serialize(ar);
      if (ar.IsStoring())
      {
            ar.WriteCount(arContact.GetSize());
      }
      else
      {
            DWORD nOldSize = ar.ReadCount();
            arContact.SetSize(nOldSize, -1);
      }
for (int i = 0; i < arContact.GetSize(); i++)
{
// serealize items in the array
}
}
0
 
LVL 12

Expert Comment

by:migel
ID: 1327421
ooops CObject::Serealize must be deleted!
0
 

Author Comment

by:RMD010799
ID: 1327422
Hmmm.... how would I go about serializing the items in the array so that I can retreive them simple by retreiving the array in which there are stored. How do I make it so that the CString objects are happy is what I am trying to say...
0
 
LVL 12

Expert Comment

by:migel
ID: 1327423
for (int i = 0; i < arContact.GetSize(); i++)
      {
      // serealize items in the array
     PContact cont = arContact[i];
     //MFC provide function to serialize CString object
    // void AFXAPI SerializeElements(CArchive& ar, CString* pElements, int nCount);
      SerializeElements(ar, &PContact.str1, 1);
      SerializeElements(ar, &PContact.str2, 1);
// and so on...
    if (!ar.IsStoring())
       arContact.SetAt(i, cont);
      }
0
 
LVL 8

Accepted Solution

by:
Answers2000 earned 200 total points
ID: 1327424
I serialize arrays using my own code,  here's example :

1. Incorporate these 2 functions in a header (e.g. stdafx.h)

template< class ARRAY, class ITEM >
void SerializeArray(CArchive& ar, ARRAY& rArray )
{
      if ( ar.IsStoring() )
      {
            ar << (int)1000 ; // version

            int nCount = rArray.GetSize() ;
            ar << nCount ;

            if ( nCount > 0 )
            {
                  for ( int ii = 0 ; ii < nCount ; ii++ )
                  {
                        ar << rArray[ii] ;
                  }
            }
      }

      else
      {
            int nVersion ;
            ar >> nVersion ;
            ASSERT( nVersion == 1000 ) ;

            rArray.RemoveAll() ;

            int nCount ;
            ar >> nCount ;

            if ( nCount > 0 )
            {
                  for ( int ii = 0 ; ii < nCount ; ii++ )
                  {
                        ITEM str ;
                        ar >> str ;
                        rArray.Add( str ) ;
                  }
            }
      }

}

template< class ARRAY, class ITEM >
void SerializeArray_Serialize(CArchive& ar, ARRAY& rArray )
{
      if ( ar.IsStoring() )
      {
            ar << (int)1000 ; // version

            int nCount = rArray.GetSize() ;
            ar << nCount ;

            if ( nCount > 0 )
            {
                  for ( int ii = 0 ; ii < nCount ; ii++ )
                  {
                        rArray[ii].Serialize( ar ) ;
                  }
            }
      }

      else
      {
            int nVersion ;
            ar >> nVersion ;
            ASSERT( nVersion == 1000 ) ;

            rArray.RemoveAll() ;

            int nCount ;
            ar >> nCount ;

            if ( nCount > 0 )
            {
                  for ( int ii = 0 ; ii < nCount ; ii++ )
                  {
                        ITEM str ;
                        str.Serialize( ar ) ;
                        rArray.Add( str ) ;
                  }
            }
      }

}


2. You can use SerializeArray function to serialize an array where the << and >> operators exist (e.g. CString) for the elements.  For example if your document contains

a m_arrayString defined as CArray<CString,CString&>

void CMailGuruDoc::Serialize(CArchive& ar)
{
      CDocument::Serialize(ar) ;

      if (ar.IsStoring())
      {
      SerializeArray< CArray<CString,CString&>, CString >( ar, m_arrayString ) ;
              } else
      {
      SerializeArray< CArray<CString,CString&>, CString >( ar, m_arrayString ) ;
              }
}


3. You can SerializeArray_Serialize if the array elements are you own class (which has the serialization methods defined)



There are shorter ways to do this, but using your own code gives you _exact_ control.        

0
 

Author Comment

by:RMD010799
ID: 1327425
Thanks for all u'r help guys.
0
 

Expert Comment

by:lipanshi
ID: 1327426
you may use' for' cycle .because you know array that count cycle number
0
 

Author Comment

by:RMD010799
ID: 1327427
Migel's example on how to serialize my array was in-fact the best answer. I wish I could give you points for it... perhaps I can. How would I go about retreiving the array that I serialize with your method from the file? I am unclear how you method saves that data... so I am unclear on how to retreive it. Thanks for your help. If you would like... I can post a new question and offer you points...
0
 

Author Comment

by:RMD010799
ID: 1327428
Also... to add to my above comment... I the code that Migel gave me, I get an "illegal use of this type" error on these lines:

SerializeElements(ar, &PContact.sEMail, 1);
SerializeElements(ar, &PContact.sIP, 1);
SerializeElements(ar, &PContact.sNick, 1);

PContact is a struct with three CStrings... sEMail, sIp and sNick.
0
 

Author Comment

by:RMD010799
ID: 1327429
Please disregard the latest post... I figured it all out. Thanks for your help... consider this question forum closed.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question