Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2016-02

Variable Initialization Questions

8 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Mazola Winstrol

Hello List,

After googled and searching in the documentation,

http://free-pascal-general.1045716.n5.nabble.com/does-Advanced-Record-constructor-automatically-zero-all-the-memory-space-of-that-record-td5716755.html http://www.freepascal.org/docs-html/ref/refse23.html

i still have doubts about variable initialization:

1 - Does the compiler automatically initialize all class fields when a new instance is created?
2 - Does the compiler initialize record fields?
3 - When i declare a pointer as a global variable or as a member of a record, can i assume that its initial value is nil? 4 - Is there any compiler switch regarding initialization of variables?

Best regards

Jonas Maebe
to Mazola Winstrol
Mazola Winstrol wrote
1 - Does the compiler automatically initialize all class fields when a new instance is created?

They're called instance fields, and yes, they are zeroed. Class fields are fields that that are unique per class type, rather than per instance.

2 - Does the compiler initialize record fields?

Only for fields that are managed types.

3 - When i declare a pointer as a global variable or as a member of a record, can i assume that its initial value is nil?

No.

4 - Is there any compiler switch regarding initialization of variables?

There is only -gt, but it "initialises" (non-managed) variables and out-parameter with random data in order to more easily find cases of uninitialised data. It cannot be used to initialise all variables to a known value.

Jonas

Mazola Winstrol
to Jonas Maebe

2016-02-17 14:58 GMT-02:00 Jonas Maebe :

Show quoted text 34 lines
Mazola Winstrol wrote: > 1 - Does the compiler automatically initialize all class fields when a > new instance is created? > They're called instance fields, and yes, they are zeroed. Class fields are fields that that are unique per class type, rather than per instance. 2 - Does the compiler initialize record fields? > Only for fields that are managed types. 3 - When i declare a pointer as a global variable or as a member of a > record, can i assume that its initial value is nil? > No. 4 - Is there any compiler switch regarding initialization of variables? > There is only -gt, but it "initialises" (non-managed) variables and out-parameter with random data in order to more easily find cases of uninitialised data. It cannot be used to initialise all variables to a known value. Jonas _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Thanks a lot for the explanation!

Regards

Jonas Maebe
to Jonas Maebe: They're called instance fields, and yes, they are zeroed. Class fields are fields that…
Jonas Maebe wrote
> 3 - When i declare a pointer as a global variable or as a member of a > record, can i assume that its initial value is nil? No.

Correction: all global variables are always zeroed, so that includes pointers, records and hence also pointers in records (if those records themselves are global variables). Pointers, records or record fields in are not zeroed because they are pointers/records/record fields though (so not in case they are local variables).

Jonas

Mazola Winstrol
to Jonas Maebe

2016-02-17 15:16 GMT-02:00 Jonas Maebe :

Show quoted text 14 lines
Jonas Maebe wrote: > 3 - When i declare a pointer as a global variable or as a member of a >> record, can i assume that its initial value is nil? >> > > No. > Correction: all global variables are always zeroed, so that includes pointers, records and hence also pointers in records (if those records themselves are global variables). Pointers, records or record fields in are not zeroed because they are pointers/records/record fields though (so not in case they are local variables).

Just to clarify: if i have a record type with a pointer field, this field will always zeroed?

e.g

type
   TMyRecord = record
   private
     FMyPointer: Pointer;
   public
     property MyPointer: Pointer read FMyPointer;
   end;

......

procedure ShowMyPointerValue;
var
  MyRecord: TMyRecord;
begin
  writeln('My pointer value: ' + IntToStr(Integer(MyRecord.MyPointer)));
end;
end.

is correct to say that the above code will always print 'My pointer value: 0'?

Regards

Jonas Maebe
to Mazola Winstrol
Mazola Winstrol wrote
Just to clarify: if i have a record type with a pointer field, this field will always zeroed?

No. As mentioned, all global variables are always zeroed. The type doesn't matter. Local variables are never zeroed, except if they are managed types. A pointer is not a managed type.

Jonas

Mazola Winstrol
to Jonas Maebe

2016-02-18 18:38 GMT-02:00 Jonas Maebe :

Show quoted text 12 lines
Mazola Winstrol wrote: > Just to clarify: if i have a record type with a pointer field, this > field will always zeroed? > No. As mentioned, all global variables are always zeroed. The type doesn't matter. Local variables are never zeroed, except if they are managed types. A pointer is not a managed type.

May i add a remark about the behavior of managed types in the documentation?

http://www.freepascal.org/docs-html/current/ref/refse23.html#x55-730004.4

Regards