Initializes a dynamically allocated variable.
procedure Initialize(var V); overload; procedure Initialize(var V; Count: Integer); overload;
Initialize( V); Initialize( V, int Count);
Initialize should be used only in Delphi code where a variable is dynamically allocated by other means than the New standard procedure.
For global variables, local variables, objects, and dynamic variables allocated using New, the compiler generates code that initializes all long strings and variants contained by a variable upon creation of the variable. A call to Initialize is required to initialize a variable before it can be used if:
Initialize simply zeros out the memory occupied by long strings, variants, and interfaces, causing long strings to be empty and variants and interfaces to be Unassigned.
In cases where several variables are allocated in a contiguous memory block, the additional Count parameter can be specified to initialize all variables in one operation.
If the variable specified in a call to Initialize contains no long strings, variants, or interfaces, the compiler eliminates the call and generates no code for it.
Delphi Examples:
{ This example demonstrates the use of Initialize and Finalize functions used to initialize and finalize a RTTI-enabled structure. } type { Declaring a data type that needs initilization } PPerson = ^TPerson; TPerson = record FFirstName: String; FLastName: String; FAge: Integer; end; procedure TForm1.Button1Click(Sender: TObject); var Person: PPerson; begin { Allocate a block of memory for the structure We're using an "unsafe" function: GetMem which doesn't use RTTI to initialize the strings inside the structure. } GetMem(Person, SizeOf(TPerson)); { Initialize the structure in the Heap - RTTI will be used } Initialize(Person^); { Assign some data to the structure } Person.FFirstName := 'John'; Person.FLastName := 'Smith'; Person.FAge := 31; { Use RTTI to cleanup the internals of the structure } Finalize(Person^); FreeMem(Person); end;
Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
|
What do you think about this topic? Send feedback!
|