Sign in Join

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

Mailing lists / fpc-pascal / 2015-02

RTFM question (?): which variables/fields are initialized by fpc?

2 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

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

Bart

Hi,

I cannot find it in the docs (probably not looking in the right place).

Which variables/class-fields are initialized by the compiler?

My understanding is that:
- global vars are initialized (zero, false, empty string etc)
- local vars are not

But what about class fields?

Typically I do

type
  TA = Class
  private
    FInt: Integer;
    FBool: Boolean;
    FString: String;
    FPoint: TPoint;
   ....
   end;
constructor TA.Create;
begin
  FInt:=0;
  FBool:=False;;
  FString:='';
  FPoint:=Point(0,0);
  ...
end;

1. Is this necessary, and if not, for which types can it be omitted? (skip if you know 2)
2. Where is it documented?
3. Does it work the same as in Delphi?

Bart

Reply by email

Michael Van Canneyt
to Bart
Bart wrote 35 lines
Hi, I cannot find it in the docs (probably not looking in the right place). Which variables/class-fields are initialized by the compiler? My understanding is that: - global vars are initialized (zero, false, empty string etc) - local vars are not But what about class fields? Typically I do type TA = Class private FInt: Integer; FBool: Boolean; FString: String; FPoint: TPoint; .... end; constructor TA.Create; begin FInt:=0; FBool:=False;; FString:=''; FPoint:=Point(0,0); ... end; 1. Is this necessary, and if not, for which types can it be omitted? (skip if you know 2)

It is not necessary. When a class is created, it's memory is zeroed out.

2. Where is it documented?

Good point. I looked for it, and it is not documented. I guess this should be added to section 6.1, will do so.

3. Does it work the same as in Delphi?

Yes.

Michael.

Reply by email