Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CArray::SetAtGrow

Status
Not open for further replies.

Warrioruw

Programmer
Jan 30, 2004
24
0
0
CA
Hi all,

In VC++ 6.0 help document, it is suggested to use SetSize before using CArray. The reason is the following:

"If you do not use SetSize, adding elements to your array causes it to be frequently reallocated and copied. Frequent reallocation and copying are inefficient and can fragment memory."

If I use SetAtGrow without setting the size of the array, do I still have inefficiency problem?


Thanks.
 
>If I use SetAtGrow without setting the size of the array, do I still have inefficiency problem?
Yes, you still to have inefficiency problem because the SetAtGrow() allocates memory depending on the index passed to this function while SetSize() with nGrowBy set is more performant because you control how much memory to increase.
CArray<double,double> arr;

arr.Add(199.00); // Element 0
arr.Add(299.10); // Element 1
// Element 2 skipped
arr.SetAtGrow(3, 399.60); // Element 3 , adjusted for 4 elements
arr.SetAtGrow(10,17.89); // Element 10, adjusted for 10 elements

but arr.SetSize(2,10); will adjust to 10 elements only once.

-obislavu-

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top