Solved

Serializing CArray

Posted on 1999-01-09
13
488 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Technology Partners: 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!

 
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 50 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

 Database Backup and Recovery Best Practices

Join Percona’s, Architect, Manjot Singh as he presents Database Backup and Recovery Best Practices (with a Focus on MySQL) on Thursday, July 27, 2017 at 11:00 am PDT / 2:00 pm EDT (UTC-7). In the case of a failure, do you know how long it will take to restore your database?

Question has a verified solution.

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

If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

622 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