Quick start manual
5-42
Delphi Language Guide
Declared constants
Dynamic variables
You can create dynamic variables by calling the GetMem or New procedure. Such 
variables are allocated on the heap and are not managed automatically. Once you 
create one, it is your responsibility ultimately to free the variable’s memory; use 
FreeMem to destroy variables created by GetMem and Dispose to destroy variables 
created by New. Other standard routines that operate on dynamic variables include 
ReallocMem, AllocMem, Initialize, Finalize, StrAlloc, and StrDispose.
Long strings, wide strings, dynamic arrays, variants, and interfaces are also heap-
allocated dynamic variables, but their memory is managed automatically.
Thread-local variables
Thread-local (or thread) variables are used in multithreaded applications. A thread-
local variable is like a global variable, except that each thread of execution gets its 
own private copy of the variable, which cannot be accessed from other threads. 
Thread-local variables are declared with threadvar instead of var. For example,
threadvar X: Integer;
Thread-variable declarations
• cannot occur within a procedure or function.
• cannot include initializations.
• cannot specify the absolute directive.
Dynamic variables that are ordinarily managed by the compiler—long strings, wide 
strings, dynamic arrays, variants, and interfaces—can be declared with threadvar, 
but the compiler does not automatically free the heap-allocated memory created by 
each thread of execution. If you use these data types in thread variables, it is your 
responsibility to dispose of their memory from within the thread, before the thread 
terminates. For example,
threadvar S: AnsiString;
S := 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
ƒ
S := ''; // free the memory used by S
Note
Use of such constructs is discouraged.
You can free a variant by setting it to Unassigned and an interface or dynamic array by 
setting it to nil.
Declared constants
Several different language constructions are referred to as “constants”. There are 
numeric constants (also called numerals) like 17, and string constants (also called 
character strings or string literals) like 'Hello world!'; for information about numeric 
and string constants, see Chapter 4, “Syntactic elements”. Every enumerated type 
defines constants that represent the values of that type. There are predefined 
constants like True, False, and nil. Finally, there are constants that, like variables, are 
created individually by declaration.










