CString::FreeExtra

voidFreeExtra();

Remarks

Call this member function to free any extra memory previously allocated by the string but no longer needed. This should reduce the memory overhead consumed by the string object. The function reallocates the buffer to the exact length returned by GetLength.

Example

The following example prints the three lines shown below under Output.

// compile with /MT or /MTd option!

#include <afx.h>

void PrintLengths(CString& str)
{
   printf("Alloc length is %d, String length is %d\n",
      str.GetAllocLength(), str.GetLength());
}

void main()
{
   // Create a CString with 1024 characters
   CString str('x', 1024);
   PrintLengths(str);

   // Assigning a smaller string won't cause CString to free its
   // memory, as it assumes the string will grow again anyway.
   str = _T("Hockey is Best!");
   PrintLengths(str);

   // This call forces CString to release the extra
   // memory it doesn't need.
   str.FreeExtra();
   PrintLengths(str);
}

Output

Alloc length is 1024, String length is 1024
Alloc length is 1024, String length is 15
Alloc length is 64, String length is  15

CString OverviewClass MembersHierarchy Chart