Global variables are zero-initialized. Variables used in the context of the main begin
..end
block of a program can be a special case; sometimes they are treated as local variables, particularly for
-loop indexers. However, in your example, r
is a global variable and allocated from the .bss section of the executable, which the Windows loader ensures is zero-filled.
Local variables are initialized as if they were passed to the Initialize
routine. The Initialize
routine uses runtime type-info (RTTI) to zero-out fields (recursively – if a field is of an array or record type) and arrays (recursively – if the element type is an array or a record) of a managed type, where a managed type is one of:
- AnsiString
- UnicodeString
- WideString
- an interface type (including method references)
- dynamic array type
- Variant
Allocations from the heap are not necessarily initialized; it depends on what mechanism was used to allocate memory. Allocations as part of instance object data are zero-filled by TObject.InitInstance
. Allocations from AllocMem
are zero-filled, while GetMem
allocations are not zero-filled. Allocations from New
are initialized as if they were passed to Initialize
.