GetMemory allocates a memory block.
function GetMemory(Size: Integer): Pointer; cdecl;
__cdecl void * GetMemory(int Size);
GetMemory allocates a block of the given Size on the heap, and returns the address of this memory. The bytes of the allocated buffer are not set to zero. To dispose of the buffer, use FreeMem. If there isn't enough memory available to allocate the block, an EOutOfMemory exception is raised.
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); }
Delphi Examples:
{ This example requires a button, a test edit, and a populated ClientDataSet. Pipe the ClientDataSet through a DataSource to a DGGrid or DBNavigator to control the current field. Cast the data correctly according to the field type when assigning to the test edit. } {$IFNDEF UNICODE} uses SwSystem; {$ENDIF} procedure TForm1.Button1Click(Sender: TObject); var MyBuffer: Pointer; begin { Retrieve the "raw" data from Field1 } with CDS.Fields[0] do begin if not IsBlob then { this does not work for BLOB fields } begin { Allocate space } MyBuffer:= GetMemory(DataSize); try if not GetData(MyBuffer) then MessageDlg(DisplayName + ' is NULL', mtInformation, [mbOK], 0) else { Do something with the data }; Edit1.Text:= string(PAnsiChar(MyBuffer)); // for a stringfield finally { Free the space } FreeMem(MyBuffer, DataSize); end; end; end; end; procedure TForm1.FormCreate(Sender: TObject); begin {$IFDEF UNICODE} CDS.LoadFromFile(GetCurrentDir + '\CDS.XML'); {$ELSE} CDS.LoadFromFile(gsAppPath + 'CDS.XML'); {$ENDIF} end;
Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
|
What do you think about this topic? Send feedback!
|