Initialization of packed record local variables?

classic Classic list List threaded Threaded
13 messages Options
Reply | Threaded
Open this post in threaded view
|

Initialization of packed record local variables?

Bo Berglund
I am getting compile hints "Local variable "<variable name>" does not
seem to be initialized" for a number of variables of packed record
type.

Does not the FPC compiler reserve and zero the memory area of such
variables?

In any case I am using Move() to fill the variables with data from a
binary buffer, maybe the compiler is not smart enough to detect this
and remove the warning?

(FPC 3.0 and Lazarus 1.6 on Raspberry Pi3)

--
Bo Berglund
Developer in Sweden

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Initialization of packed record local variables?

Michael Van Canneyt


On Mon, 18 Jul 2016, Bo Berglund wrote:

> I am getting compile hints "Local variable "<variable name>" does not
> seem to be initialized" for a number of variables of packed record
> type.
>
> Does not the FPC compiler reserve and zero the memory area of such
> variables?

No.


> In any case I am using Move() to fill the variables with data from a
> binary buffer, maybe the compiler is not smart enough to detect this
> and remove the warning?

The compiler has no way of knowing if your move statement initializes the
full record.

Michael.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Initialization of packed record local variables?

Bo Berglund
On Mon, 18 Jul 2016 21:52:37 +0200 (CEST), Michael Van Canneyt
<[hidden email]> wrote:

>> Does not the FPC compiler reserve and zero the memory area of such
>> variables?
>
>No.

Does thios mean that the memory is reserved but not cleared or does it
mean that memory is not reserved at all?

>
>> In any case I am using Move() to fill the variables with data from a
>> binary buffer, maybe the compiler is not smart enough to detect this
>> and remove the warning?
>
>The compiler has no way of knowing if your move statement initializes the
>full record.
>
Well, if it has no way of knowing that the record is stuffed with data
then why does it issue a notice that it seems uninitialized?
Are there other commands it does detect as initialization of the
variable?


--
Bo Berglund
Developer in Sweden

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Initialization of packed record local variables?

Michael Van Canneyt


On Mon, 18 Jul 2016, Bo Berglund wrote:

> On Mon, 18 Jul 2016 21:52:37 +0200 (CEST), Michael Van Canneyt
> <[hidden email]> wrote:
>
>>> Does not the FPC compiler reserve and zero the memory area of such
>>> variables?
>>
>> No.
>
> Does thios mean that the memory is reserved but not cleared or does it
> mean that memory is not reserved at all?


the memory is reserved, but not initialized.

>
>>
>>> In any case I am using Move() to fill the variables with data from a
>>> binary buffer, maybe the compiler is not smart enough to detect this
>>> and remove the warning?
>>
>> The compiler has no way of knowing if your move statement initializes the
>> full record.
>>
> Well, if it has no way of knowing that the record is stuffed with data
> then why does it issue a notice that it seems uninitialized?
> Are there other commands it does detect as initialization of the
> variable?

not that I know of.

you need to assign it a value, you can assign it default(yourtype)

Michael.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Initialization of packed record local variables?

Bo Berglund
On Mon, 18 Jul 2016 23:30:47 +0200 (CEST), Michael Van Canneyt
<[hidden email]> wrote:

>> Are there other commands it does detect as initialization of the
>> variable?
>
>not that I know of.
>
>you need to assign it a value, you can assign it default(yourtype)
>

OK, I did not know about the default keyword.

Is "default" zero?
i.e.

var
  HD: TSSReadingHeader;
  ..
begin
  HD := Default(TSSReadingHeader);

And now HD.anything is zero?


--
Bo Berglund
Developer in Sweden

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Initialization of packed record local variables?

leledumbo
Administrator
> OK, I did not know about the default keyword.
>
> Is "default" zero?

http://wiki.lazarus.freepascal.org/FPC_New_Features_3.0#New_compiler_intrinsic_Default
Reply | Threaded
Open this post in threaded view
|

Re: Initialization of packed record local variables?

Michael Van Canneyt


On Fri, 22 Jul 2016, leledumbo wrote:

>> OK, I did not know about the default keyword.
>>
>> Is "default" zero?
>
> http://wiki.lazarus.freepascal.org/FPC_New_Features_3.0#New_compiler_intrinsic_Default

Or better yet, the official documentation:

http://www.freepascal.org/docs-html/current/rtl/system/default.html

Michael.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Initialization of packed record local variables?

Mattias Gaertner
On Fri, 22 Jul 2016 11:03:23 +0200 (CEST)
Michael Van Canneyt <[hidden email]> wrote:

> On Fri, 22 Jul 2016, leledumbo wrote:
>
> >> OK, I did not know about the default keyword.
> >>
> >> Is "default" zero?  
> >
> > http://wiki.lazarus.freepascal.org/FPC_New_Features_3.0#New_compiler_intrinsic_Default 
>
> Or better yet, the official documentation:
>
> http://www.freepascal.org/docs-html/current/rtl/system/default.html

Can the examples be added to official docs?

Mattias

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Initialization of packed record local variables?

Michael Van Canneyt


On Fri, 22 Jul 2016, Mattias Gaertner wrote:

> On Fri, 22 Jul 2016 11:03:23 +0200 (CEST)
> Michael Van Canneyt <[hidden email]> wrote:
>
>> On Fri, 22 Jul 2016, leledumbo wrote:
>>
>>>> OK, I did not know about the default keyword.
>>>>
>>>> Is "default" zero?
>>>
>>> http://wiki.lazarus.freepascal.org/FPC_New_Features_3.0#New_compiler_intrinsic_Default
>>
>> Or better yet, the official documentation:
>>
>> http://www.freepascal.org/docs-html/current/rtl/system/default.html
>
> Can the examples be added to official docs?

Good point, will do !

Michael.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Initialization of packed record local variables?

Mark Morgan Lloyd-5
In reply to this post by Michael Van Canneyt
Michael Van Canneyt wrote:

> Or better yet, the official documentation:
>
> http://www.freepascal.org/docs-html/current/rtl/system/default.html

Question please. Using the official documentation as linked to above,
how does one find out what versions of the compiler/rtl/fcl support
something?

--
Mark Morgan Lloyd
markMLl .AT. telemetry.co .DOT. uk

[Opinions above are the author's, not those of his employers or colleagues]
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Initialization of packed record local variables?

Michael Van Canneyt


On Fri, 22 Jul 2016, Mark Morgan Lloyd wrote:

> Michael Van Canneyt wrote:
>
>> Or better yet, the official documentation:
>>
>> http://www.freepascal.org/docs-html/current/rtl/system/default.html
>
> Question please. Using the official documentation as linked to above, how
> does one find out what versions of the compiler/rtl/fcl support something?

One does not - yet.

I am working on leaving the various versions available (notice the 'current' in the URL)
and then you could view previous versions...

Michael.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Initialization of packed record local variables?

Bo Berglund
In reply to this post by Bo Berglund
On Fri, 22 Jul 2016 08:12:01 +0200, Bo Berglund
<[hidden email]> wrote:

>>you need to assign it a value, you can assign it default(yourtype)
>>
>
>Is "default" zero?
>i.e.
>
>var
>  HD: TSSReadingHeader;
>  ..
>begin
>  HD := Default(TSSReadingHeader);
>
>And now HD.anything is zero?

I made a test once I got the other issues solved and indeed Default()
on my packed records seems to zero out the memory!
Very good to know! :)

Thanks for pointing it out!

--
Bo Berglund
Developer in Sweden

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Initialization of packed record local variables?

Mark Morgan Lloyd-5
In reply to this post by Michael Van Canneyt
Michael Van Canneyt wrote:

> On Fri, 22 Jul 2016, Mark Morgan Lloyd wrote:
>
>> Michael Van Canneyt wrote:
>>
>>> Or better yet, the official documentation:
>>>
>>> http://www.freepascal.org/docs-html/current/rtl/system/default.html
>>
>> Question please. Using the official documentation as linked to above,
>> how does one find out what versions of the compiler/rtl/fcl support
>> something?
>
> One does not - yet.
>
> I am working on leaving the various versions available (notice the
> 'current' in the URL)
> and then you could view previous versions...

I started hacking through trying to produce at least a list of
functions/procedures and a permuted index from different versions, but I
got bogged down in an apparent requirement that the documentation files
needed to be processed by the corresponding version of supplied
utilities. I'm sure what I did was of little value since I was attacking
the problem with insufficient understanding of "the right way to do it"
and I'm not even sure where it's got to.

--
Mark Morgan Lloyd
markMLl .AT. telemetry.co .DOT. uk

[Opinions above are the author's, not those of his employers or colleagues]
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal