Sign in Join

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

Mailing lists / fpc-pascal / 2013-09

does Advanced Record constructor automatically zero all the memory space of that record?

8 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

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

Dennis Poon

if not, is there a clean and easy way to initialize the entire record memory space to zeros e.g.

constructor TMyRecord.Create(TheValue : Integer);
begin
   FillChar(self, sizeof(Self), 0);
   Value := TheVal;
end;

Is there a Self variable for advanced record methods/constructors ?

By the way, is there an ultimate inherited constructor "Create" for all advanced record types?

I cannot find any info by googling, so had to ask for your help here.

Thanks in advanced.

Dennis

Reply by email

Dmitry Boyarintsev
to Dennis Poon

If in doubt, why not to use the old-school records and an init-value procedure.

thanks,
Dmitry

Dennis Poon wrote 22 lines
** if not, is there a clean and easy way to initialize the entire record memory space to zeros e.g. constructor TMyRecord.Create(TheValue : Integer); begin FillChar(self, sizeof(Self), 0); Value := TheVal; end; Is there a Self variable for advanced record methods/constructors ? By the way, is there an ultimate inherited constructor "Create" for all advanced record types? I cannot find any info by googling, so had to ask for your help here. Thanks in advanced. Dennis

Reply by email

Sven Barth
to Dennis Poon: if not, is there a clean and easy way to initialize the entire record memory space to…
does Advanced Record constructor automatically zero all the memory space of that record?

No, it does not. Neither in Delphi nor in FPC.

Dennis Poon wrote 8 lines
if not, is there a clean and easy way to initialize the entire record memory space to zeros e.g. constructor TMyRecord.Create(TheValue : Integer); begin FillChar(self, sizeof(Self), 0); Value := TheVal; end;

You can use either FillChar as you did or use "Self := Default(TTest);" (default is a compiler intrinsics that returns a 0 value of the type you passed in, e.g. Nil for classes, '' for strings, 0 for ordinals and for records all fields are set to 0)

Is there a Self variable for advanced record methods/constructors ?

Yes.

By the way, is there an ultimate inherited constructor "Create" for all advanced record types?

No.

Regards,
Sven

Reply by email

Dennis Poon
to Sven Barth
Sven Barth wrote 26 lines
> does Advanced Record constructor automatically zero all the memory > space of that record? No, it does not. Neither in Delphi nor in FPC. Am 24.09.2013 12:36, schrieb Dennis Poon: > if not, is there a clean and easy way to initialize the entire record > memory space to zeros e.g. > > constructor TMyRecord.Create(TheValue : Integer); > begin > FillChar(self, sizeof(Self), 0); > Value := TheVal; > end; You can use either FillChar as you did or use "Self := Default(TTest);" (default is a compiler intrinsics that returns a 0 value of the type you passed in, e.g. Nil for classes, '' for strings, 0 for ordinals and for records all fields are set to 0) > Is there a Self variable for advanced record methods/constructors ? Yes. > By the way, is there an ultimate inherited constructor "Create" for > all advanced record types? No. Regards, Sven

Thanks for your help.

Are the above info. available anywhere in FPC's web site that I overlooked? If not, maybe it should be added to its wiki.

Dennis

Reply by email

Sven Barth
to Dennis Poon
Dennis Poon wrote 34 lines
Sven Barth wrote: >> does Advanced Record constructor automatically zero all the memory >> space of that record? > No, it does not. Neither in Delphi nor in FPC. > > Am 24.09.2013 12:36, schrieb Dennis Poon: >> if not, is there a clean and easy way to initialize the entire >> record memory space to zeros e.g. >> >> constructor TMyRecord.Create(TheValue : Integer); >> begin >> FillChar(self, sizeof(Self), 0); >> Value := TheVal; >> end; > You can use either FillChar as you did or use "Self := > Default(TTest);" (default is a compiler intrinsics that returns a 0 > value of the type you passed in, e.g. Nil for classes, '' for > strings, 0 for ordinals and for records all fields are set to 0) >> Is there a Self variable for advanced record methods/constructors ? > Yes. >> By the way, is there an ultimate inherited constructor "Create" for >> all advanced record types? > No. > > Regards, > Sven > Thanks for your help. Are the above info. available anywhere in FPC's web site that I overlooked? If not, maybe it should be added to its wiki.

Both points should be added to the documentation, but since they both are implemented only in trunk this is not the case yet.

Default() is also mentioned here:
http://wiki.lazarus.freepascal.org/FPC_New_Features_Trunk#New_compiler_intrinsic_Default

Regards,
Sven

Reply by email

Dennis Poon
to Sven Barth
You can use either FillChar as you did or use "Self :=

Default(TTest);" (default is a compiler intrinsics that returns a 0 value of the type >you passed in, e.g. Nil for classes, '' for strings, 0 for ordinals and for records all fields are set to 0)

How can I make it so that I only do the Self := Default() once in the parent class constructor and don't have to repeat it in all its descendant class constructor.

I noticed in your example, you have to specify the class name TTest but that implies in every descendant class, I have to replace it with the Descendant class name.

Can I do

   self := Default(ClassType) ?

hoping it will return the descendant's class type?

Dennis

Reply by email

Sven Barth
to Dennis Poon
Dennis Poon wrote 10 lines
>You can use either FillChar as you did or use "Self := Default(TTest);" (default is a compiler intrinsics that returns a 0 value of the type >you passed in, e.g. Nil for classes, '' for strings, 0 for ordinals and for records all fields are set to 0) How can I make it so that I only do the Self := Default() once in the parent class constructor and don't have to repeat it in all its descendant class constructor.

Why would you want to do that? Classes are already zero initialized when the constructor is entered.
Also using "Self := Default(...)" inside a class would not do what you expect it to do. "Self" is a class reference and thus "Default(...)" of the same type would return "Nil" and thus set your "Self" for the current method call to "Nil" (so you could no longer access the class instance at all).

Show quoted text 7 lines
I noticed in your example, you have to specify the class name TTest but that implies in every descendant class, I have to replace it with the Descendant class name. Can I do self := Default(ClassType) ? hoping it will return the descendant's class type?

Default expects a type identifier and nothing else. I currently don't know whether Delphi supports more. But in any way it wouldn't change much as Default(TObject) = Default(TSomeClass) =
Default(TSomeChildClass) = Nil.

Regards,
Sven

Reply by email

Dennis Poon
to Sven Barth

Thanks a lot for clarification.

Dennis

Sven Barth wrote 46 lines
On 04.10.2013 14:48, Dennis Poon wrote: > > > >You can use either FillChar as you did or use "Self := > Default(TTest);" (default is a compiler intrinsics that returns a 0 > value of the type >you passed in, e.g. Nil for classes, '' for strings, > 0 for ordinals and for records all fields are set to 0) > > How can I make it so that I only do the Self := Default() once in the > parent class constructor and don't have to repeat it in all its > descendant class constructor. Why would you want to do that? Classes are already zero initialized when the constructor is entered. Also using "Self := Default(...)" inside a class would not do what you expect it to do. "Self" is a class reference and thus "Default(...)" of the same type would return "Nil" and thus set your "Self" for the current method call to "Nil" (so you could no longer access the class instance at all). > I noticed in your example, you have to specify the class name TTest but > that implies in every descendant class, I have to replace it with the > Descendant class name. > > Can I do > self := Default(ClassType) ? > hoping it will return the descendant's class type? Default expects a type identifier and nothing else. I currently don't know whether Delphi supports more. But in any way it wouldn't change much as Default(TObject) = Default(TSomeClass) = Default(TSomeChildClass) = Nil. Regards, Sven _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal ----- No virus found in this message. Checked by AVG - www.avg.com Version: 2013.0.3408 / Virus Database: 3222/6723 - Release Date: 10/04/13

Reply by email