Share via


CArray::SetSize

Establishes the size of an empty or existing array; allocates memory if necessary.

void SetSize( 
   INT_PTR nNewSize, 
   INT_PTR nGrowBy = -1  
);

Parameters

  • nNewSize
    The new array size (number of elements). Must be greater than or equal to 0.

  • nGrowBy
    The minimum number of element slots to allocate if a size increase is necessary.

Remarks

If the new size is smaller than the old size, then the array is truncated and all unused memory is released.

Use this function to set the size of your array before you begin using the array. 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.

The nGrowBy parameter affects internal memory allocation while the array is growing. Its use never affects the array size as reported by GetSize and GetUpperBound. If the default value is used, MFC allocates memory in a way calculated to avoid memory fragmentation and optimize efficiency for most cases.

Example

See the example for GetData.

Requirements

Header: afxtempl.h

See Also

Reference

CArray Class

Hierarchy Chart

CArray::GetUpperBound

CArray::GetSize

CArray::GetCount

Other Resources

CArray Members