CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Guest

    CArray elements access

    Help! I define that:

    struct FILEINFO{...};

    CArray<FILEINFO,FILEINFO> m_ptArray;

    So, for adding elements i use

    FILEINFO fi;
    m_ptArray.Add(fi);

    BUT !!! I get wrong data when try to access any index number except [0]. However, size of CArray reported by debugger still growing. What's wrong i did?
    Anyone, please...


  2. #2
    Guest

    Re: CArray elements access

    I dont know Why CArray size is growing. But to get the correct data, try this definition

    CArray <FILEINFO, FILEINFO&> ... to get reference pointer for struct data


  3. #3
    Join Date
    Apr 1999
    Posts
    4

    Re: CArray elements access

    When you add your structure to the Array, your structure object is going out of scope.

    This is how you should declare your CArray:

    CArray<FILEINFO, &FILEINFO> m_ptArray;
    This allows you to store pointers.

    then do this to make a FILEINFO:

    FILEINFO * fi = new FILEINFO;
    m_ptArray.Add(fi);

    By making a new structure, you are creating the data on the heap which is local to the whole program, versus creating data on the function's stack. As soon as the function that does this:

    FILEINFO fi;
    m_ptArray.Add(fi);

    is over, fi is not valid anymore. That is why you have to use the new operator to create it on the heap.

    Remember, when you use the GetAt operator, you have to cast the pointer you get back. a Simpler way is to used the CTypedPtrArray. The declaration would be like this:

    CTypedPtrArray<CPtrArray, FILEINFO*> m_ptArray;

    Then you don't have to cast everything.

    Make sure you look in MSDN for proper ways to clean up CArrays...

    Hope this helps...
    kevin


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