RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
System.ReallocMemory Function

ReallocMemory resizes a memory block.

Pascal
function ReallocMemory(P: Pointer; Size: Integer): Pointer; cdecl;
C++
__cdecl void * ReallocMemory(void * P, int Size);

GetMem allocates a block of the given Size on the heap. If you need to change the size of this memory block, call ReallocMemory, passing the existing memory block pointer in P, and the revised block size in Size. A pointer to the resized memory block is returned. If ReallocMemory can't expand the memory block pointed to by P, it frees the referenced memory and copies the values to the newly allocated memory that is returned.  

If there isn't enough memory available to extend the memory block to the desired size, an EOutOfMemory exception is raised.

Note: ReallocMemory is the C++ compatible version of ReallocMem.
 

C++ Examples: 

 

/*
The following code demostrates the use of memory management functions in C++. Three
edit boxes and a button are expected onthe form.
*/
String __fastcall FastStrCat(String s1, String s2)
{
  /*
   Allocated enough space in FinalStr to copy the contents
   of the initial string + $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);

  // And now expand the final string + $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);
}

 

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!