Sign in Join

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

Mailing lists / fpc-pascal / 2016-12

how to use Default to replace this " FillChar(aRecord, sizeof(aRecord), 0);

7 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

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

Dennis

In this old statement, I don't need to know the type of aRecord but if I want to use Default, I have to know the exact type of the varaible aRecord and pass it to Default(aRecordType) e.g. aRecord := Default(aRecordType) //where var aRecord : aRecordType

Is it possible to use default without knowing the variable type? (it is because sometimes the type get renamed)

Dennis

Lars
to Dennis

Do you know about this feature:

var
  SomeRec: TSomeRecord = ();

This initializes the record without needing fillchar. I assume your "Default()" call initializes the record initially. You can eliminate the default() call by initializing the record as part of your declaration.

Some criticize this as being C++ like and not pascalish. I find it to be a contract set up at the declaration stage without needing for ugly fillchar hacks which seem like a mess... But old pascal programmers are used to and like fillchar.

However, my email here may have nothing to do with what you are trying to accomplish... As you want a some kind of generic function to fillchar any record, I guess... My solution above, is to use () to fillchar it to 0 without requiring any call to fillchar. If you want to add values other than zero then you can set them up inside the initialization instead of a blank ()

An interesting trivia note: delphi 5 doesn't seem to allow initialized record declarations in local variables inside a procedure but allows it for globals or unit variables...

Also the fpc compiler gives a warning:
Warning: Some fields coming after "" were not initialized

When using () to initialize a record... I'm not sure if this warning is correct, as theoretically the compiler should in fact set all record values to zero in the case of ()

Should this warning be looked at as a possible erroneous warning?

Again, sorry if my answer to your email has nothing at all to do with what you are trying to accomplish!

Dennis wrote 15 lines
In this old statement, I don't need to know the type of aRecord but if I want to use Default, I have to know the exact type of the varaible aRecord and pass it to Default(aRecordType) e.g. aRecord := Default(aRecordType) //where var aRecord : aRecordType Is it possible to use default without knowing the variable type? (it is because sometimes the type get renamed) Dennis _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Sven Barth
"Dennis" wrote
In this old statement, I don't need to know the type of aRecord but if I want to use Default, I have to know the exact type of the
varaible aRecord and pass it to Default(aRecordType) e.g. aRecord :=
Default(aRecordType)  //where var  aRecord : aRecordType
Is it possible to use default without knowing the variable type? (it is

because sometimes the type get renamed)

No, it's not possible. You'll simply have to rename one more location.

Regards,
Sven

Dennis Poon
to Sven Barth
Sven Barth wrote 18 lines
Am 05.12.2016 08:24 schrieb "Dennis" < >: > > In this old statement, I don't need to know the type of aRecord > but if I want to use Default, I have to know the exact type of the varaible aRecord and pass it to Default(aRecordType) e.g. aRecord := Default(aRecordType) //where var aRecord : aRecordType > > Is it possible to use default without knowing the variable type? (it is because sometimes the type get renamed) No, it's not possible. You'll simply have to rename one more location. Regards, Sven

Thanks.

Lars
to Dennis Poon
Dennis Poon wrote 14 lines
Lars wrote: > Do you know about this feature: > > > var SomeRec: TSomeRecord = (); > This is good for global variable. But I wonder whether it has effect in procedure's local variable since it requires the compiler to clear the record on the stack (which is at different location every time).

I tested it and it seems to compile, and then the value is in fact 0 when I try to access it and do a showmessage(inttostr(rec.i))... where rec.i is an integer...

But just because it compiles and just because it is zero this time, does not mean.. it is consistent. So this needs to be confirmed.

It is weird that fpc allows a local initialized var to compile, but delphi 5 rejects it... Delphi 5 is older and may have not had as advanced stack technology as fpc ;-)

Another trick is to setup a default global, and then just assign the local var to the global (copying the contents) which resets the local to the global.. i.e. if you assign one global record () default 0 contents, to a local... using:

var
  GlobalDefault := TSomeRec = ();
procedure ...
var
  local: TSomeRec = ();
begin
  localrec := GlobalDefault
Sven Barth
to Lars
"Lars" wrote 23 lines
On Mon, December 5, 2016 4:46 am, Dennis Poon wrote: > > > Lars wrote: > >> Do you know about this feature: >> >> >> var SomeRec: TSomeRecord = (); >> > This is good for global variable. > But I wonder whether it has effect in procedure's local variable since > it requires the compiler to clear the record on the stack (which is at > different location every time). I tested it and it seems to compile, and then the value is in fact 0 when I try to access it and do a showmessage(inttostr(rec.i))... where rec.i is an integer... But just because it compiles and just because it is zero this time, does not mean.. it is consistent. So this needs to be confirmed.

I wouldn't trust the initialization of missing fields (after all the compiler warns about it) as it's undocumented/undefined behavior.

It is weird that fpc allows a local initialized var to compile, but delphi 5 rejects it... Delphi 5 is older and may have not had as advanced stack technology as fpc ;-)

It's a feature of FPC. It simply adds an approbiate assignment at the entry of the function.

Show quoted text 13 lines
Another trick is to setup a default global, and then just assign the local var to the global (copying the contents) which resets the local to the global.. i.e. if you assign one global record () default 0 contents, to a local... using: var GlobalDefault := TSomeRec = (); procedure ... var local: TSomeRec = (); begin localrec := GlobalDefault

Just use Default(TSomeRec) and be done with it... -.-

Regards,
Sven