System MoveChars (C++)

From RAD Studio Code Examples
Jump to: navigation, search

Description

The following code demostrates the use of memory management functions in C++. Three edit boxes and a button are required on the form.

Code

String __fastcall FastStrCat(String s1, String s2)
{
  /*
   Allocated enough space in FinalStr to copy the contents
   of the initial string, plus the $00 character
  */
  wchar_t* finalStr = (wchar_t *)GetMemory((s1.Length() + 1) * sizeof(wchar_t));

  // Copy the contents of the first string.
  MoveChars(s1.data(), finalStr, s1.Length() + 1);

  // Now expand the final string, plus the $00 character.
  finalStr = (wchar_t *)ReallocMemory(finalStr, (s1.Length() + s2.Length() + 1) * sizeof(wchar_t));

  // Copy the contents of the second string.
  MoveChars(s2.data(), finalStr + s1.Length(), s2.Length() + 1);

  // Get the result in String.
  String result = String(finalStr);
  FreeMemory(finalStr);

  return result;
}

void __fastcall TForm1::Button1Click(TObject *Sender)
{
  // Concatenate 2 string
  Edit3->Text = FastStrCat(Edit1->Text, Edit2->Text);
}

Uses