Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. CArchive and long*

CArchive and long*

Scheduled Pinned Locked Moved C / C++ / MFC
question
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    eusto
    wrote on last edited by
    #1

    I want to serialize an object that has a long* member. How do i do that?

    C 1 Reply Last reply
    0
    • E eusto

      I want to serialize an object that has a long* member. How do i do that?

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      Obviously you cannot serialize it in that format (it will only serialize the pointer, not the contents). What you need to do when saving your data is serialize the contents (for example, if this is an array of long, then serialize the number of elements and then serialize each element). Then, for loading, do the opposite operation: load the number of elements (and then allocate your array), then load each element.


      Cédric Moonen Software developer
      Charting control [v1.1]

      E 1 Reply Last reply
      0
      • C Cedric Moonen

        Obviously you cannot serialize it in that format (it will only serialize the pointer, not the contents). What you need to do when saving your data is serialize the contents (for example, if this is an array of long, then serialize the number of elements and then serialize each element). Then, for loading, do the opposite operation: load the number of elements (and then allocate your array), then load each element.


        Cédric Moonen Software developer
        Charting control [v1.1]

        E Offline
        E Offline
        eusto
        wrote on last edited by
        #3

        I knew that it would not help me much to serialize the pointer to my long list. This is what i do:

        void CMyObj::Serialize(CArchive& a)
        {
        if (ar.IsStoring())
        {
        ar<>m_nCount;
        m_list = new long[m_nCount];
        for(int i = 0;i>m_list[i];
        }
        }
        }

        What am i doing wrong? m_nCount gets read corectly but m_list is always filled whith 0's :(

        M 1 Reply Last reply
        0
        • E eusto

          I knew that it would not help me much to serialize the pointer to my long list. This is what i do:

          void CMyObj::Serialize(CArchive& a)
          {
          if (ar.IsStoring())
          {
          ar<>m_nCount;
          m_list = new long[m_nCount];
          for(int i = 0;i>m_list[i];
          }
          }
          }

          What am i doing wrong? m_nCount gets read corectly but m_list is always filled whith 0's :(

          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #4

          eusto wrote:

          What am i doing wrong? m_nCount gets read corectly but m_list is always filled whith 0's

          This should work. Are you sure you are not writing an array of 0s? m_list is declared as a "long *"?

          void CMyObj::Serialize(CArchive& ar)
          {
          if (ar.IsStoring())
          {
          ar << m_nCount;
          for(int i = 0; i < m_nCount; i++)
          ar << m_list[i];
          }
          else
          {
          ar >> m_nCount;
          m_list = new long[m_nCount];
          for(int i = 0; i < m_nCount; i++)
          {
          ar >> m_list[i];
          }
          }
          }

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups