[fpc-pascal] Initializing records and other complicated structures

Honza befelemepeseveze at gmail.com
Mon Sep 27 10:38:16 CEST 2010


2010/9/27 Jim <hakkie42 at gmail.com>:
> 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



More information about the fpc-pascal mailing list