ReallocMem reallocates a memory block.
procedure ReallocMem(var P: Pointer; Size: Integer);
ReallocMem(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 ReallocMem, passing the existing memory block pointer in P, and the revised block size in Size. If ReallocMem 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.
Delphi Examples:
{ The following code demostrates the use of ReallocMem function. Three edit boxes and a button are expected onthe form. } function FastStrCat(const S1, S2: String): String; var FinalStr: PChar; begin { Allocated enough space in FinalStr to copy the contents of the initial string + $00 character } GetMem(FinalStr, (Length(S1) + 1) * SizeOf(Char)); { Copy the contents of the first string } MoveChars(S1[1], FinalStr^, Length(S1) + 1); { And now expand the final string + $00 character } ReallocMem(FinalStr, (Length(S1) + Length(S2) + 1) * SizeOf(Char)); { Copy the contents of the second string } MoveChars(S2[1], FinalStr[Length(S1)], Length(S2) + 1); { Get the result in String } SetString(Result, FinalStr, Length(S1) + Length(S2)); FreeMem(FinalStr); end; procedure TForm1.Button1Click(Sender: TObject); begin { Concatenate 2 string } Edit3.Text := FastStrCat(Edit1.Text, Edit2.Text); end;
Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
|
What do you think about this topic? Send feedback!
|