Sign in Join

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

Mailing lists / fpc-pascal / 2010-09

Initializing records and other complicated structures

10 messages · 7 people · –

Reply by email Subscribe to this list Your subscription

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

Jim

Hi,

Fpc sometimes complains (hints) about local variables not being initialized. My question: how do I initialize more complicated structures like records? And should I need to (I'm guessing no in the example below, but the contents of the record will be undefined - no problem as there are no pointers/strings in there)?

Example:
....
Type
  TIfdTag = packed Record
    ID: word;
    //Tag number
    Typ: word;
    //Type tag
    Count: cardinal;
    //tag length
    Offset: cardinal;
    //Offset / Value
  End;
....
Var
  tag: TIfdTag;
....
in code somewhere
               ReadTag(tag);

As TIfdTag seems to contain non-pointer records only, I thought I could use Sizeof and tried this:
FillByte(tag, SizeOf(tag), 0); //initialize var.
The compiler warning doesn't go away though.

Is this the recommended way of doing things?

Thanks,

Regards, jb

Reply by email

Honza
to Jim

2010/9/27 Jim :

Show quoted text 32 lines
Fpc sometimes complains (hints) about local variables not being initialized. My question: how do I initialize more complicated structures like records? And should I need to (I'm guessing no in the example below, but the contents of the record will be undefined - no problem as there are no pointers/strings in there)? Example: .... Type  TIfdTag = packed Record    ID: word;    //Tag number    Typ: word;    //Type tag    Count: cardinal;    //tag length    Offset: cardinal;    //Offset / Value  End; .... Var  tag: TIfdTag; .... in code somewhere               ReadTag(tag); As TIfdTag seems to contain non-pointer records only, I thought I could use Sizeof and tried this: FillByte(tag, SizeOf(tag), 0); //initialize var. The compiler warning doesn't go away though. Is this the recommended way of doing things?

There is not enough code shown to say much. If the ReadTag procedure is declared with a presumably var paramater of type TIfdTag then the compiler warning is OK if the passed record has not been initialized in any way. In such case it may/should help to change the 'var' paramater to an 'out' parameter to explicitly tell the compiler that there is nothing going into ReadTag but only the other way.

HTH

bflm freepascal-bits.blogspot.com

Reply by email

Sven Barth
to Honza
Honza wrote 14 lines
2010/9/27 Jim: > As TIfdTag seems to contain non-pointer records only, I thought I could > use Sizeof and tried this: > FillByte(tag, SizeOf(tag), 0); //initialize var. > The compiler warning doesn't go away though. > > Is this the recommended way of doing things? There is not enough code shown to say much. If the ReadTag procedure is declared with a presumably var paramater of type TIfdTag then the compiler warning is OK if the passed record has not been initialized in any way. In such case it may/should help to change the 'var' paramater to an 'out' parameter to explicitly tell the compiler that there is nothing going into ReadTag but only the other way.

It's enough code, because FillByte (and others) are defined with a "var" parameter and such the hint (not a warning) won't go away. It's an old topic already and we have just to live with it (regarding the Fill* functions). ^^

Regards,
Sven

Reply by email

Jim
to Sven Barth
Sven Barth wrote 24 lines
Am 27.09.2010 10:38, schrieb Honza: > 2010/9/27 Jim: >> As TIfdTag seems to contain non-pointer records only, I thought I could >> use Sizeof and tried this: >> FillByte(tag, SizeOf(tag), 0); //initialize var. >> The compiler warning doesn't go away though. >> >> Is this the recommended way of doing things? > > There is not enough code shown to say much. If the ReadTag procedure > is declared with a presumably var paramater of type TIfdTag then the > compiler warning is OK if the passed record has not been initialized > in any way. In such case it may/should help to change the 'var' > paramater to an 'out' parameter to explicitly tell the compiler that > there is nothing going into ReadTag but only the other way. It's enough code, because FillByte (and others) are defined with a "var" parameter and such the hint (not a warning) won't go away. It's an old topic already and we have just to live with it (regarding the Fill* functions). ^^ Regards, Sven

Thanks you two, that helped!

Regards, jb

Reply by email

Jürgen Hestermann
to Sven Barth: It's enough code, because FillByte (and others) are defined with a "var" parameter and…
It's enough code, because FillByte (and others) are defined with a "var" parameter and such the hint (not a warning) won't go away. It's an old topic already and we have just to live with it (regarding the Fill* functions). ^^

But why not simply change the definition of the variable to "out"? Then the Fill* functions would be recognized as initializing routines.

Reply by email

Vincent Snijders
to Jürgen Hestermann

2010/9/28 Jürgen Hestermann :

Show quoted text 9 lines
Sven Barth schrieb: > > It's enough code, because FillByte (and others) are defined with a "var" > parameter and such the hint (not a warning) won't go away. > It's an old topic already and we have just to live with it (regarding the > Fill* functions). ^^ But why not simply change the definition of the variable to "out"? Then the Fill* functions would be recognized as initializing routines.

No, see http://bugs.freepascal.org/view.php?id=17020

Vincent

Reply by email

Sven Barth
to Vincent Snijders
Vincent Snijders wrote 12 lines
2010/9/28 Jürgen Hestermann: > Sven Barth schrieb: >> >> It's enough code, because FillByte (and others) are defined with a "var" >> parameter and such the hint (not a warning) won't go away. >> It's an old topic already and we have just to live with it (regarding the >> Fill* functions). ^^ > > But why not simply change the definition of the variable to "out"? Then the > Fill* functions would be recognized as initializing routines. No, see http://bugs.freepascal.org/view.php?id=17020

@Jürgen:
That's what I meant, when I said it's an old topic. ;)

Regards,
Sven

Reply by email

Graeme Geldenhuys
to Jim: Fpc sometimes complains (hints) about local variables not being initialized. My question:…
Jim wrote
FillByte(tag, SizeOf(tag), 0); //initialize var. The compiler warning doesn't go away though.

I've had the same problem a few monts ago, and I hate compiler hints/warnings in my code. The issue is the var parameters, and the FPC developers already stated that they will not change the Fill*() functions parameters to being out parameters (some Windows OLE or COM thing). What I had to do was create my own wrapper function for Fill(), then inside that wrapper function I called FillChar or FillByte etc, and that call is wrapped by {$HINT OFF} and {$HINT ON} directives. A bit of a pain, but it worked, and I silenced the compiler warning.

Regards,   - Graeme - _______________________________________________ fpGUI - a cross-platform Free Pascal GUI toolkit http://opensoft.homeip.net:8080/fpgui/

Reply by email

Unknown author

It seems that we need a new parameter modifier "out_non_finalize" since "out" includes a finalization.

We had a similar discussion with "constref".

Maybe we need a comprehensive method to describe calling conventions, see e.g. http://mantis.freepascal.org/view.php?id=16697 or the discussion about "in out" parameters: http://lists.freepascal.org/lists/fpc-devel/2010-August/021452.html

GCC has some stuff like that for inline assembly, see e.g. the paragraph "Extended inline assembly" in http://www.delorie.com/djgpp/doc/brennan/brennan_att_inline_djgpp.html ___________________________________________________________ WEB.DE DSL SOMMER-SPECIAL: Surf & Phone Flat 16.000 für nur 19,99 €/mtl.!* http://produkte.web.de/go/DSL_Doppel_Flatrate/2

Reply by email

Jürgen Hestermann
to Sven Barth: @Jürgen: That's what I meant, when I said it's an old topic. ;) Sven
Show quoted text 6 lines
>> But why not simply change the definition of the variable to "out"? >> Then the >> Fill* functions would be recognized as initializing routines. > No, see http://bugs.freepascal.org/view.php?id=17020 @Jürgen: That's what I meant, when I said it's an old topic. ;)

Yes, I see now.
Over a longer time the link above did not work for me (error 400 or so) so I could not look at it and therefore did not know that "out" is no solution.

Reply by email