Sign in Join

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

Mailing lists / fpc-pascal / 2014-01

how to initialize a SearchRec var?

23 messages · 9 people · –

Reply by email Subscribe to this list Your subscription

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

waldo kitty

understanding that SearchRec has different formats for different OS targets, we've tried to use fillchar to initialize it to an empty record... unfortunately that does not remove the hint... instead the hint points to the fillchar line...

we do not want to turn off hints so how can we initialize dirinfo and make fpc happy?

[quote="os/2 ecs"]
Hint: Start of reading config file fpc.cfg
Hint: End of reading config file fpc.cfg
Free Pascal Compiler version 2.6.2 [2013/02/09] for i386 Copyright (c) 1993-2012 by Florian Klaempfl and others Target OS: OS/2
Compiling dirinfo.pas
dirinfo.pas(13,19) Hint: Variable "dirinfo" does not seem to be initialized Assembling test_searchrec
Linking dirinfo.exe
emxbind 0.9d -- Copyright (c) 1991-1997 by Eberhard Mattes Deleting *path*\projects\satsort\dirinfo.out
1 file deleted 335,872 bytes freed
16 lines compiled, 3.9 sec
3 hint(s) issued
[/quote]

[quote]="vista"]
C:\freepascal\projects\misc_projects>fpc -vhinw dirinfo.pas Hint: Start of reading config file fpc.cfg
Hint: End of reading config file fpc.cfg
Free Pascal Compiler version 2.7.1 [2013/12/05] for i386 Copyright (c) 1993-2013 by Florian Klaempfl and others Target OS: Win32 for i386
Compiling dirinfo.pas
dirinfo.pas(13,19) Hint: Variable "dirinfo" does not seem to be initialized Linking dirinfo.exe
16 lines compiled, 0.8 sec, 28304 bytes code, 1524 bytes data 3 hint(s) issued
[/quote]

[quote="dirinfo.pas"]
program test_SearchRec;
uses
   Dos;
const
   pattern : string = '*.pas';
var
   dirinfo : SearchRec;
begin
   fillchar(dirinfo,sizeof(dirinfo),$00);
   FindFirst(pattern,AnyFile,dirinfo);
   findclose(dirinfo);
end.
[/quote]
NOTE: No off-list assistance is given without prior approval. Please keep mailing list traffic on the list unless private contact is specifically requested and granted.
Jonas Maebe
to waldo kitty
waldo kitty wrote
understanding that SearchRec has different formats for different OS targets, we've tried to use fillchar to initialize it to an empty record... unfortunately that does not remove the hint... instead the hint points to the fillchar line...

It is virtually impossible to get a realistic program free of any hints. Hints are the lowest possible verbosity level and their only goal is to help you pinpoint extremely unlikely sources of problems if you've exhausted all other options. They cause false positives by design (in this case: passing an uninitialized variable to a var-parameter; and since fillchar's first parameter is such a parameter and cannot be changed to "out" for reasons discussed many times before on this list, using that routine indeed does not help).

Jonas

Florian Klämpfl
to waldo kitty: understanding that SearchRec has different formats for different OS targets, we've tried…
waldo kitty wrote 9 lines
understanding that SearchRec has different formats for different OS targets, we've tried to use fillchar to initialize it to an empty record... unfortunately that does not remove the hint... instead the hint points to the fillchar line... we do not want to turn off hints so how can we initialize dirinfo and make fpc happy?

With 2.7+ you can use Default (in objfpc mode):

  dirinfo:=Default(SearchRec);
Sven Barth
to waldo kitty: understanding that SearchRec has different formats for different OS targets, we've tried…
waldo kitty wrote 63 lines
understanding that SearchRec has different formats for different OS targets, we've tried to use fillchar to initialize it to an empty record... unfortunately that does not remove the hint... instead the hint points to the fillchar line... we do not want to turn off hints so how can we initialize dirinfo and make fpc happy? [quote="os/2 ecs"] Hint: Start of reading config file fpc.cfg Hint: End of reading config file fpc.cfg Free Pascal Compiler version 2.6.2 [2013/02/09] for i386 Copyright (c) 1993-2012 by Florian Klaempfl and others Target OS: OS/2 Compiling dirinfo.pas dirinfo.pas(13,19) Hint: Variable "dirinfo" does not seem to be initialized Assembling test_searchrec Linking dirinfo.exe emxbind 0.9d -- Copyright (c) 1991-1997 by Eberhard Mattes Deleting *path*\projects\satsort\dirinfo.out 1 file deleted 335,872 bytes freed 16 lines compiled, 3.9 sec 3 hint(s) issued [/quote] [quote]="vista"] C:\freepascal\projects\misc_projects>fpc -vhinw dirinfo.pas Hint: Start of reading config file fpc.cfg Hint: End of reading config file fpc.cfg Free Pascal Compiler version 2.7.1 [2013/12/05] for i386 Copyright (c) 1993-2013 by Florian Klaempfl and others Target OS: Win32 for i386 Compiling dirinfo.pas dirinfo.pas(13,19) Hint: Variable "dirinfo" does not seem to be initialized Linking dirinfo.exe 16 lines compiled, 0.8 sec, 28304 bytes code, 1524 bytes data 3 hint(s) issued [/quote] [quote="dirinfo.pas"] program test_SearchRec; uses Dos; const pattern : string = '*.pas'; var dirinfo : SearchRec; begin fillchar(dirinfo,sizeof(dirinfo),$00); FindFirst(pattern,AnyFile,dirinfo); findclose(dirinfo); end. [/quote]

Beginning with 2.7.1 you can use the new Default() intrinsic which takes a type name as first and only argument (it returns a 0 initialized value of that type):

=== code begin ===

var
   dirinfo: SearchRec;
begin
   dirinfo := Default(SearchRec);
   FindFirst(pattern, AnyFile, dirinfo);
   FindClose(dirinfo);
end;

=== code end ===

Regards,
Sven

Sven Barth
to Florian Klämpfl: With 2.7+ you can use Default (in objfpc mode): dirinfo:=Default(SearchRec);
Florian Klämpfl wrote 14 lines
Am 02.01.2014 20:21, schrieb waldo kitty: > > > understanding that SearchRec has different formats for different OS > targets, we've tried to use fillchar to initialize it to an empty > record... unfortunately that does not remove the hint... instead the > hint points to the fillchar line... > > we do not want to turn off hints so how can we initialize dirinfo and > make fpc happy? With 2.7+ you can use Default (in objfpc mode): dirinfo:=Default(SearchRec);

It does not need mode ObjFPC, Default() is supported in any mode (as it's a "system magic function" like Length(), High(), etc..

Regards,
Sven

waldo kitty
to Sven Barth: Beginning with 2.7.1 you can use the new Default() intrinsic which takes a type name as…
Sven Barth wrote
Beginning with 2.7.1 you can use the new Default() intrinsic which takes a type name as first and only argument (it returns a 0 initialized value of that type):

2.7.1 which means that we still have to respond to queries with older versions... ok, i think we can deal with that...

NOTE: No off-list assistance is given without prior approval. Please keep mailing list traffic on the list unless private contact is specifically requested and granted.
waldo kitty
to Mark Morgan Lloyd: Out of curiosity, does this work: var dirinfo: SearchRec= Default(SearchRec); Mark Morgan…
Mark Morgan Lloyd wrote 12 lines
Sven Barth wrote: > === code begin === > > var > dirinfo: SearchRec; > begin > dirinfo := Default(SearchRec); Out of curiosity, does this work: var dirinfo: SearchRec= Default(SearchRec);

+1 ;)

what i run into is porting old code and trying to possbly also make it multi-platform... the biggest gotcha we've run into is those typed constants that are used to hold values between executions of a routine... those should be initialized to a zero/empty value before first execution but then retain their values afterward...

NOTE: No off-list assistance is given without prior approval. Please keep mailing list traffic on the list unless private contact is specifically requested and granted.
Graeme Geldenhuys
to waldo kitty: understanding that SearchRec has different formats for different OS targets, we've tried…
waldo kitty wrote
we do not want to turn off hints so how can we initialize dirinfo and make fpc happy?

I hate such hints too! This is what I did in my code to prevent that unwanted hint.

One part is the usage of an out parameter, and the second part is the
{$HINT OFF} directive.
----8<-------------8<-------------8<-------------8<-------------8<----
program test_SearchRec;
uses
   Dos;
const
   pattern : string = '*.pas';
var
   dirinfo : SearchRec;
// prevents the compiler hint about not initialized variables
procedure dcpFillChar(out x; count: SizeInt; Value: Byte);
begin
  {$HINTS OFF}
  FillChar(x, count, value);
  {$HINTS ON}
end;
begin
   dcpFillchar(dirinfo,sizeof(dirinfo),$00);
   FindFirst(pattern,AnyFile,dirinfo);
   findclose(dirinfo);
end.
----8<-------------8<-------------8<-------------8<-------------8<----

And here is the compiler output:

[tmp]$ fpc -viwnh test_SearchRec.pas
Hint: Start of reading config file /home/graemeg/.fpc.cfg Hint: End of reading config file /home/graemeg/.fpc.cfg Free Pascal Compiler version 2.6.2 [2013/02/10] for x86_64 Copyright (c) 1993-2012 by Florian Klaempfl and others Target OS: FreeBSD for x86-64
Compiling test_SearchRec.pas
Linking test_SearchRec
25 lines compiled, 0.0 sec
2 hint(s) issued

Regards,
- Graeme -

fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal http://fpgui.sourceforge.net/
Mark Morgan Lloyd
to Sven Barth
Sven Barth wrote 16 lines
On 02.01.2014 22:27, Mark Morgan Lloyd wrote: > Sven Barth wrote: >> === code begin === >> >> var >> dirinfo: SearchRec; >> begin >> dirinfo := Default(SearchRec); > > Out of curiosity, does this work: > > var > dirinfo: SearchRec= Default(SearchRec); > Not yet. It's on my ToDo list however since I implemented Default().

That would be useful. I'm currently trying to do a line-by-line transcription of a CPU emulator that somebody's been misguided enough to write in JavaScript. Being able to deal concisely with things like

this.memMod = [ // Array of memory module words as Float64s (8 x 4KW each)

         null, null, null, null, null, null, null, null];

would be a decided advantage.

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]
Sven Barth
to Mark Morgan Lloyd

Am 03.01.2014 08:36 schrieb "Mark Morgan Lloyd" <

Show quoted text 26 lines
: Sven Barth wrote: > > On 02.01.2014 22:27, Mark Morgan Lloyd wrote: >> >> Sven Barth wrote: >>> >>> === code begin === >>> >>> var >>> dirinfo: SearchRec; >>> begin >>> dirinfo := Default(SearchRec); >> >> >> Out of curiosity, does this work: >> >> var >> dirinfo: SearchRec= Default(SearchRec); >> > > Not yet. It's on my ToDo list however since I implemented Default(). That would be useful. I'm currently trying to do a line-by-line

transcription of a CPU emulator that somebody's been misguided enough to write in JavaScript. Being able to deal concisely with things like

this.memMod = [ // Array of memory module words as Float64s (8 x 4KW

each)

null, null, null, null, null, null, null, null]; would be a decided advantage.

In case of arrays you can already do the following:

=== code begin ===

const
  NullMemMod: array[0..7] of Pointer(* or whatever *) = (Nil, Nil, Nil,
Nil, Nil, Nil, Nil, Nil);
// somewhere else:
Self.MemMod := NullMemMod;

=== code end ===

Regards,
Sven

Sven Barth
to Graeme Geldenhuys: I hate such hints too! This is what I did in my code to prevent that unwanted hint. One…
"Graeme Geldenhuys" wrote
On 2014-01-02 19:21, waldo kitty wrote: > > we do not want to turn off hints so how can we initialize dirinfo and

make fpc

Show quoted text 38 lines
> happy? I hate such hints too! This is what I did in my code to prevent that unwanted hint. One part is the usage of an out parameter, and the second part is the {$HINT OFF} directive. ----8<-------------8<-------------8<-------------8<-------------8<---- program test_SearchRec; uses Dos; const pattern : string = '*.pas'; var dirinfo : SearchRec; // prevents the compiler hint about not initialized variables procedure dcpFillChar(out x; count: SizeInt; Value: Byte); begin {$HINTS OFF} FillChar(x, count, value); {$HINTS ON} end; begin dcpFillchar(dirinfo,sizeof(dirinfo),$00); FindFirst(pattern,AnyFile,dirinfo); findclose(dirinfo); end. ----8<-------------8<-------------8<-------------8<-------------8<----

I would suggest to use the following sequence instead:

=== code begin ===

{$push}
{$hints off}
// whatever
{$pop}

=== code end ===

This way if the hints are switched off before that code is reached they will stay off afterwards instead of being switched on.

Regards,
Sven

Pierre Free Pascal
to Sven Barth
Show quoted text 16 lines
// prevents the compiler hint about not initialized variables procedure dcpFillChar(out x; count: SizeInt; Value: Byte); begin   {$HINTS OFF}   FillChar(x, count, value);   {$HINTS ON} end; begin    dcpFillchar(dirinfo,sizeof(dirinfo),$00);    FindFirst(pattern,AnyFile,dirinfo);    findclose(dirinfo); end. ----8<-------------8<-------------8<-------------8<-------------8<----
I would suggest to use the following sequence instead:
=== code begin ===
{$push}
{$hints off}
// whatever
{$pop}
=== code end ===
  The is a more specific way to remove a given hint:

Use -vq option to get the numeric value of the message:

E:\pas\test>ppc386 -vhq tsearchrec.pas
Hint: (11030) Start of reading config file
e:\pas\fpc-2.7.1\bin\i386-win32\fpc.cfg
Hint: (11031) End of reading config file
e:\pas\fpc-2.7.1\bin\i386-win32\fpc.cfg
tsearchrec.pas(13,20) Hint: (5058) Variable "dirinfo" does not seem to be initialized

Disable that specific message by using -vm option:

E:\pas\test>ppc386 -vh -vm5058 tsearchrec.pas
Hint: Start of reading config file e:\pas\fpc-2.7.1\bin\i386-win32\fpc.cfg Hint: End of reading config file e:\pas\fpc-2.7.1\bin\i386-win32\fpc.cfg

Magic, isn't it?

Pierre Muller

Sven Barth
to Pierre Free Pascal
"Pierre Free Pascal" wrote 44 lines
> // prevents the compiler hint about not initialized variables > procedure dcpFillChar(out x; count: SizeInt; Value: Byte); > begin > {$HINTS OFF} > FillChar(x, count, value); > {$HINTS ON} > end; > > > begin > > dcpFillchar(dirinfo,sizeof(dirinfo),$00); > FindFirst(pattern,AnyFile,dirinfo); > findclose(dirinfo); > end. > ----8<-------------8<-------------8<-------------8<-------------8<---- I would suggest to use the following sequence instead: === code begin === {$push} {$hints off} // whatever {$pop} === code end === The is a more specific way to remove a given hint: Use -vq option to get the numeric value of the message: E:\pas\test>ppc386 -vhq tsearchrec.pas Hint: (11030) Start of reading config file e:\pas\fpc-2.7.1\bin\i386-win32\fpc.cfg Hint: (11031) End of reading config file e:\pas\fpc-2.7.1\bin\i386-win32\fpc.cfg tsearchrec.pas(13,20) Hint: (5058) Variable "dirinfo" does not seem to be initialized Disable that specific message by using -vm option: E:\pas\test>ppc386 -vh -vm5058 tsearchrec.pas Hint: Start of reading config file e:\pas\fpc-2.7.1\bin\i386-win32\fpc.cfg Hint: End of reading config file e:\pas\fpc-2.7.1\bin\i386-win32\fpc.cfg Magic, isn't it?

Maybe one should also mention that this can be done in code as well for specific locations:

=== code begin ===
{$push}
{$warn 5058 off}
// whatever
{$pop}
=== code end ===

Regards,
Sven

Jürgen Hestermann
to Sven Barth
Sven Barth wrote 7 lines
Maybe one should also mention that this can be done in code as well for specific locations: === code begin === {$push} {$warn 5058 off} // whatever {$pop} === code end ===

It seems it can be made still easier by right-clicking on the message in the message window and by selecting "Hide message via directive". Then a {%H-} is appended to the variable that caused the message which does not add additional lines of code.

Sven Barth
to Jürgen Hestermann: It seems it can be made still easier by right-clicking on the message in the message…
"Jürgen Hestermann" wrote
Am 2014-01-03 12:07, schrieb Sven Barth: > Maybe one should also mention that this can be done in code as well for

specific locations:

Show quoted text 12 lines
> === code begin === > {$push} > {$warn 5058 off} > // whatever > {$pop} > === code end === It seems it can be made still easier by right-clicking on the message in the message window and by selecting "Hide message via directive". Then a {%H-} is appended to the variable that caused the message which does not add additional lines of code.

This only works inside Lazarus and only tells the IDE to ignore hints from that position (roughly). If you compile on the command line the hint will resurface. Also it does not seem that one is able to control which type of hint is hidden.

Regards,
Sven

Bart

(Sending again: the mailserver bounced it the first time.)

Jürgen Hestermann wrote
It seems it can be made still easier by right-clicking on the message in the message window and by selecting "Hide message via directive". Then a {%H-} is appended to the variable that caused the message which does not add additional lines of code.

That's a Lazarus thing isn't it?
AFAIK it doen't suppress the compiler hint, it just doesn't show it in the Messages window.

Bart

waldo kitty
to waldo kitty: 2.7.1 which means that we still have to respond to queries with older versions... ok, i…
waldo kitty wrote 6 lines
On 1/2/2014 2:52 PM, Sven Barth wrote: > Beginning with 2.7.1 you can use the new Default() intrinsic which takes a type > name as first and only argument (it returns a 0 initialized value of that type): 2.7.1 which means that we still have to respond to queries with older versions... ok, i think we can deal with that...

following up on this... this is what i came up with... is it right/proper?

   {$IF (FPC_FULLVERSION >= 20701)}
   {$INFO initializing dirinfo with Default(SearchRec)}
   dirinfo := default(searchrec);
   {$ELSE}
   {$PUSH}
   {$HINTS OFF}
   {$INFO initializing dirinfo with fillchar(dirinfo,sizeof(dirinfo),$00)}
   fillchar(dirinfo,sizeof(dirinfo),$00);
   {$POP}
   {$ENDIF}
NOTE: No off-list assistance is given without prior approval. Please keep mailing list traffic on the list unless private contact is specifically requested and granted.
Sven Barth
to waldo kitty
waldo kitty wrote 26 lines
On 1/2/2014 6:31 PM, waldo kitty wrote: > On 1/2/2014 2:52 PM, Sven Barth wrote: >> Beginning with 2.7.1 you can use the new Default() intrinsic which >> takes a type >> name as first and only argument (it returns a 0 initialized value of >> that type): > > 2.7.1 which means that we still have to respond to queries with older > versions... ok, i think we can deal with that... following up on this... this is what i came up with... is it right/proper? {$IF (FPC_FULLVERSION >= 20701)} {$INFO initializing dirinfo with Default(SearchRec)} dirinfo := default(searchrec); {$ELSE} {$PUSH} {$HINTS OFF} {$INFO initializing dirinfo with fillchar(dirinfo,sizeof(dirinfo),$00)} fillchar(dirinfo,sizeof(dirinfo),$00); {$POP} {$ENDIF}

Should be correct.

Regards,
Sven

waldo kitty
to Sven Barth
Sven Barth wrote 18 lines
On 05.01.2014 19:24, waldo kitty wrote: > following up on this... this is what i came up with... is it right/proper? > > {$IF (FPC_FULLVERSION >= 20701)} > {$INFO initializing dirinfo with Default(SearchRec)} > dirinfo := default(searchrec); > {$ELSE} > {$PUSH} > {$HINTS OFF} > {$INFO initializing dirinfo with fillchar(dirinfo,sizeof(dirinfo),$00)} > fillchar(dirinfo,sizeof(dirinfo),$00); > {$POP} > {$ENDIF} > > > Should be correct.

thanks! it seems to work on the few systems where i tested it... we have to ensure that we get it placed into our existing code base ;)

NOTE: No off-list assistance is given without prior approval. Please keep mailing list traffic on the list unless private contact is specifically requested and granted.