Click to See Complete Forum and Search --> : CArray elements access
April 10th, 1999, 09:18 PM
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...
April 11th, 1999, 03:58 AM
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
kmtambasc98
April 11th, 1999, 01:16 PM
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
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.