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. CStringArray reference

CStringArray reference

Scheduled Pinned Locked Moved C / C++ / MFC
databasehelpquestion
6 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.
  • _ Offline
    _ Offline
    _Flaviu
    wrote on last edited by
    #1

    I have a problem into gather information into CStringArray reference : first , I define a CMap object :

    CMap m_mapType;

    and I insert a new element :

    CString sTemp = \_T("test value");
    CStringArray\* psaTest = new CStringArray;
    psaTest->Add(sTemp);
    m\_mapType\[\_T("index")\] = psaTest;
    psaTest->RemoveAll();
    delete psaTest;
    

    and when I try to read this element :

    CStringArray\* psaRes = new CStringArray;;
    if(m\_mapType.Lookup(\_T("index"),psaRes))AfxMessageBox("Found index");
    else AfxMessageBox("Not found");
    
    if(psaRes->GetSize())AfxMessageBox(psaRes->GetAt(0));
    else AfxMessageBox("Empty psaRes");
    
    delete psaRes;
    

    my code says that I found CMap element , but psaRes is empty , why ? Because I put wrong CStringArray reference in Lookup method ? Thank you . P.S. I put here entire code , because I try above code in the same place :

    CString sTemp = \_T("test value");
    CStringArray\* psaTest = new CStringArray;
    psaTest->Add(sTemp);
    m\_mapType\[\_T("index")\] = psaTest;
    psaTest->RemoveAll();
    delete psaTest;
    
    CStringArray\* psaRes = new CStringArray;;
    
    if(m\_mapType.Lookup(\_T("index"),psaRes))AfxMessageBox("Found index");
    else AfxMessageBox("Not found");
    
    if(psaRes->GetSize())AfxMessageBox(psaRes->GetAt(0));
    else AfxMessageBox("Empty psaRes");
    
    delete psaRes;
    
    C C 2 Replies Last reply
    0
    • _ _Flaviu

      I have a problem into gather information into CStringArray reference : first , I define a CMap object :

      CMap m_mapType;

      and I insert a new element :

      CString sTemp = \_T("test value");
      CStringArray\* psaTest = new CStringArray;
      psaTest->Add(sTemp);
      m\_mapType\[\_T("index")\] = psaTest;
      psaTest->RemoveAll();
      delete psaTest;
      

      and when I try to read this element :

      CStringArray\* psaRes = new CStringArray;;
      if(m\_mapType.Lookup(\_T("index"),psaRes))AfxMessageBox("Found index");
      else AfxMessageBox("Not found");
      
      if(psaRes->GetSize())AfxMessageBox(psaRes->GetAt(0));
      else AfxMessageBox("Empty psaRes");
      
      delete psaRes;
      

      my code says that I found CMap element , but psaRes is empty , why ? Because I put wrong CStringArray reference in Lookup method ? Thank you . P.S. I put here entire code , because I try above code in the same place :

      CString sTemp = \_T("test value");
      CStringArray\* psaTest = new CStringArray;
      psaTest->Add(sTemp);
      m\_mapType\[\_T("index")\] = psaTest;
      psaTest->RemoveAll();
      delete psaTest;
      
      CStringArray\* psaRes = new CStringArray;;
      
      if(m\_mapType.Lookup(\_T("index"),psaRes))AfxMessageBox("Found index");
      else AfxMessageBox("Not found");
      
      if(psaRes->GetSize())AfxMessageBox(psaRes->GetAt(0));
      else AfxMessageBox("Empty psaRes");
      
      delete psaRes;
      
      C Offline
      C Offline
      Cool_Dev
      wrote on last edited by
      #2

      Your CMap holds the pointers to CStringArray. You should enure that those pointers are valid till you retrieve them from map. Here you are deleting CStringArray pointers after inserting them to map. Obviously you will not get what you actually inserted. Delete the pointers only when the map is no longer needed.

      _ 1 Reply Last reply
      0
      • _ _Flaviu

        I have a problem into gather information into CStringArray reference : first , I define a CMap object :

        CMap m_mapType;

        and I insert a new element :

        CString sTemp = \_T("test value");
        CStringArray\* psaTest = new CStringArray;
        psaTest->Add(sTemp);
        m\_mapType\[\_T("index")\] = psaTest;
        psaTest->RemoveAll();
        delete psaTest;
        

        and when I try to read this element :

        CStringArray\* psaRes = new CStringArray;;
        if(m\_mapType.Lookup(\_T("index"),psaRes))AfxMessageBox("Found index");
        else AfxMessageBox("Not found");
        
        if(psaRes->GetSize())AfxMessageBox(psaRes->GetAt(0));
        else AfxMessageBox("Empty psaRes");
        
        delete psaRes;
        

        my code says that I found CMap element , but psaRes is empty , why ? Because I put wrong CStringArray reference in Lookup method ? Thank you . P.S. I put here entire code , because I try above code in the same place :

        CString sTemp = \_T("test value");
        CStringArray\* psaTest = new CStringArray;
        psaTest->Add(sTemp);
        m\_mapType\[\_T("index")\] = psaTest;
        psaTest->RemoveAll();
        delete psaTest;
        
        CStringArray\* psaRes = new CStringArray;;
        
        if(m\_mapType.Lookup(\_T("index"),psaRes))AfxMessageBox("Found index");
        else AfxMessageBox("Not found");
        
        if(psaRes->GetSize())AfxMessageBox(psaRes->GetAt(0));
        else AfxMessageBox("Empty psaRes");
        
        delete psaRes;
        
        C Offline
        C Offline
        Cedric Moonen
        wrote on last edited by
        #3

        You are putting a pointer to a CStringArray in your map, which means that it always points to the same instance of CStringArray that you were originally using (it's not a copy). And you do something like this:

        psaTest->RemoveAll();

        just after adding the pointer to the map. So, your CStringArray will be empty. By the way, you shouldn't do something like this:

        CStringArray* psaRes = new CStringArray;
        ...
        ...
        m_mapType.Lookup(_T("index"),psaRes);

        Because in that case you will have a memory leak. There's no need to make a new CStringArray: if the pointer is found in the map, it will be copied into the psaRes pointer (the address will be copied, not the content of course). BTW, I suggest you take a look at the STL containers (map, list, ...) they are much easier to use (once you are confortable with the syntax).

        Cédric Moonen Software developer
        Charting control [v3.0] OpenGL game tutorial in C++

        _ 1 Reply Last reply
        0
        • C Cool_Dev

          Your CMap holds the pointers to CStringArray. You should enure that those pointers are valid till you retrieve them from map. Here you are deleting CStringArray pointers after inserting them to map. Obviously you will not get what you actually inserted. Delete the pointers only when the map is no longer needed.

          _ Offline
          _ Offline
          _Flaviu
          wrote on last edited by
          #4

          Let's say that somewhere I serialize the map object , how can I retrieve CStringArray values ? Because there I simulate something like that.

          C 1 Reply Last reply
          0
          • C Cedric Moonen

            You are putting a pointer to a CStringArray in your map, which means that it always points to the same instance of CStringArray that you were originally using (it's not a copy). And you do something like this:

            psaTest->RemoveAll();

            just after adding the pointer to the map. So, your CStringArray will be empty. By the way, you shouldn't do something like this:

            CStringArray* psaRes = new CStringArray;
            ...
            ...
            m_mapType.Lookup(_T("index"),psaRes);

            Because in that case you will have a memory leak. There's no need to make a new CStringArray: if the pointer is found in the map, it will be copied into the psaRes pointer (the address will be copied, not the content of course). BTW, I suggest you take a look at the STL containers (map, list, ...) they are much easier to use (once you are confortable with the syntax).

            Cédric Moonen Software developer
            Charting control [v3.0] OpenGL game tutorial in C++

            _ Offline
            _ Offline
            _Flaviu
            wrote on last edited by
            #5

            Thank you for advice , much people say to me to use STL , I didn't work with , I try to keep all in MFC way .

            1 Reply Last reply
            0
            • _ _Flaviu

              Let's say that somewhere I serialize the map object , how can I retrieve CStringArray values ? Because there I simulate something like that.

              C Offline
              C Offline
              Cool_Dev
              wrote on last edited by
              #6

              CMap is derived from CObject, thereby Serialize method is there. I am not pretty sure what will happen if the map members are pointers, i mean whether the data it points to get serialized or the pointer itself. However its just a matter of a test app to verify the case. :)

              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