Recent

Author Topic: Const record partial init  (Read 2357 times)

Wallaby

  • Jr. Member
  • **
  • Posts: 78
Const record partial init
« on: January 20, 2022, 10:54:03 pm »
I am declaring a global constant like this:
Code: Pascal  [Select][+][-]
  1. const
  2.   CenterStyle: TTextStyle = (Alignment: taCenter; Layout: tlCenter; SingleLine: True);
The compiler produces a warning:
Code: Text  [Select][+][-]
  1. Warning: Some fields coming after "SingleLine" were not initialized

Does this mean the remaining members of the record may contain junk or will be zeroed?

My understanding is that if declare a global variable, it's set to (zero/False/nil) on startup. If I declare a local variable, it's not initialized and contains junk.

In this case it's a global constant, so I am guessing the rest of it will zero-filled. Can anyone confirm?

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Const record partial init
« Reply #1 on: January 20, 2022, 11:45:51 pm »
Global variables are documented as being initialized with zeros.
A typed constant such as your CenterStyle is not a "global variable" in the sense that it is not declared in a var section. However, I think such "constants" are placed in the data segment.

Anyway, my testing shows that such constants with default {$J} setting are initialized to zero.
So the booleans in the record following CenterStyle.SingleLine are all False (at least on Linux).
A compiler dev would need to say whether this is just coincidental, or whether it is a reliable FPC implementation detail.

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: Const record partial init
« Reply #2 on: January 21, 2022, 11:54:47 am »
I think you should not rely on such implementation details.
Just initialize it properly.
Also, other persons who read the code, then can immediately understand the value of all fields.

Bart

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: Const record partial init
« Reply #3 on: January 21, 2022, 12:08:38 pm »
As everybody else says, don't rely on implementation details which might change.

If you know for a fact that later fields are not going to be used, then (arguably) you can omit them. If not, initialise them even if to an empty/null state.

It's unfortunate that while (I think) it's documented that a managed type such as a string or dynamic array will implicitly be initialised empty, you can't rely on a string field in a record constant behaving in the same way hence the knock-on effect to later fields.

So even if the record contained a sequence of strings (with nothing after them), the first which wasn't explicitly initialised would cause all later ones to raise at least a warning. Now it might be that that's unintentional and that the FPC developers would look favourably on a bug report, but even if that were fixed you'd have enormous problems if somebody tried to compile your code using a slightly earlier version of the compiler.

MarkMLl
« Last Edit: January 21, 2022, 02:02:26 pm by MarkMLl »
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: Const record partial init
« Reply #4 on: January 21, 2022, 03:59:09 pm »
It's unfortunate that while (I think) it's documented that a managed type such as a string or dynamic array will implicitly be initialised empty, you can't rely on a string field in a record constant behaving in the same way hence the knock-on effect to later fields.

Managed types are always initialized to known values.

The point here is that the record declaration requires each and every field. This way if one changes the record type (e.g. by adding a new field) one can see right away where something is still missing (which might lead to subtle bugs depending on how the record value is used).

A typed constant such as your CenterStyle is not a "global variable" in the sense that it is not declared in a var section. However, I think such "constants" are placed in the data segment.

Typed constants are placed in either the data or rdata segment depending on the $J directive.

 

TinyPortal © 2005-2018