Sign in Join

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

Mailing lists / fpc-pascal / 2020-03

Re: New Warnings with fpc >= 3.2.0 (fredvs)

11 messages · 5 people ·

Reply by email Subscribe to this list Your subscription

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

KBruno Id2

A possibility, using your enumeration is to change it from

tabulatorkindty = (tak_left,tak_right,tak_centered,tak_decimal,
tak_none);

to

tabulatorkindty = (tak_none,tak_left,tak_right,tak_centered,tak_decimal);

so a field in an object is by default initialized to tak_none during the object's creation. (I generally define mynenum_undef or  mynenum_unknown as first item in an enum). This way there is no need to do an unnecessary transtyping. Enum items are represented by an unsigned 0 based CARDINAL. (AFAIK)

Reply by email

fredvs
to KBruno Id2

Hello BrunoK

so a field in an object is by default initialized to tak_none during the object's creation.

Hum, doing this:
_________________

PROGRAM enumtest;
type
    tabulatorkindty =
(tak_none,tak_left,tak_right,tak_centered,tak_decimal);
var
  kind: tabulatorkindty;
begin
    if kind = tak_none then
    WriteLn('OK it is tak_none')
  else WriteLn('No is is not tak_none') ;
end.
______________________

Gives as result:

$ enumtest

OK it is tak_none

_____________________

So, thanks for the tip, "kind" seems indeed initialized with first item.

And with your tip, one more candidate for code to choose/use to fix the bug.

Many thanks BrunoK.

Fre;D

-----
Many thanks ;-)

Reply by email

DougC
to fredvs

Note that if you depend on this technique you should add a comment where the type is defined that the order of the items is significant so that someone in the future does not change the order out of ignorance!

Reply by email

fredvs
to DougC
Note that if you depend on this technique you should add a comment where the

type is defined that the order of the items is

significant so that someone in the future does not change the order out of ignorance!

Yes, that is the main problem.
If you do it the "logical" way, placing at first without initialization, when ord(kind) is used in code, because the order has changed, lot of code must be re-written.

But if you place it at last you only have to initialize at creation with nk_none and replace all use of "high(enumty)" by the last - 1 item of the enum.

Fre;D

-----
Many thanks ;-)

Reply by email

Unknown author
to fredvs: type is defined that the order of the items is Yes, that is the main problem. If you do…
fredvs wrote
Yes, that is the main problem. If you do it the "logical" way, placing at first without initialization, when ord(kind) is used in code, because the order has changed, lot of code must be re-written.

i don't see a problem with that... only some time to fix ugly/bad code...

But if you place it at last you only have to initialize at creation with nk_none and replace all use of "high(enumty)" by the last - 1 item of the enum.

the problem with this is "do you leave it last always and insert new values before it?" OR "do you put it last now and leave it there when adding new values after it?"

much better to put it first in the list for numerous reasons...

NOTE: No off-list assistance is given without prior approval. *Please keep mailing list traffic on the list where it belongs!*

Reply by email

fredvs
to Sven Barth: fredvs via fpc-pascal schrieb am Sa., 14. März 2020, 13:01: Please note that this is…
And it's not initialized with the first item, but with 0.
Well said, indeed, that example (see tak_none := 1):
PROGRAM enumtest;
type
    tabulatorkindty =
(tak_none := 1,tak_left,tak_right,tak_centered,tak_decimal);
var
  kind: tabulatorkindty;
begin
    if kind = tak_none then
    WriteLn('OK it is tak_none')
  else WriteLn('No is is not tak_none') ;
   if kind = tabulatorkindty(0) then
    WriteLn('OK it is 0')
  else WriteLn('No is is not 0') ;
end.

___________

Gives as result:

$ /home/fred/enumtest
No it is not tak_none OK it is 0

And, cherry on the cake, now at compilation, there is that warning:

Compiling /home/fred/weird/enumtest.pas enumtest.pas(10,8) Warning: Variable "kind" does not seem to be initialized

Hum, I feel to become a enum-guru.

Thanks Sven!

Fre;D

-----
Many thanks ;-)

Reply by email

fredvs
to fredvs: Well said, indeed, that example (see tak_none := 1): PROGRAM enumtest; type…

Hi Sven, sorry to come back, (dont forget I am a enum-guru now).

But to be totally logic, in the previous code:

if kind = tabulatorkindty(0) then

Should it not be something illegal because of definition:

tabulatorkindty = (tak_none := 1,tak_left,tak_right,tak_centered,tak_decimal);

the values assigned are tak_none = 1, tak_left = 2, tak_right = 3, tak_centered = 4, tak_decimal = 5
and 0 is never assigned.

Then (re-imho) there is something strange with initialization with 0 because 0 should be out of range.

Fre;D

-----
Many thanks ;-)

Reply by email

Sven Barth
to fredvs
fredvs wrote 15 lines
Hi Sven, sorry to come back, (dont forget I am a enum-guru now). But to be totally logic, in the previous code: > if kind = tabulatorkindty(0) then Should it not be something illegal because of definition: > tabulatorkindty = > (tak_none := 1,tak_left,tak_right,tak_centered,tak_decimal); the values assigned are tak_none = 1, tak_left = 2, tak_right = 3, tak_centered = 4, tak_decimal = 5 and 0 is never assigned. Then (re-imho) there is something strange with initialization with 0 because 0 should be out of range.

The thing about the default value is that it's some known value, not necessarily some legal value. Also what value should the compiler select otherwise? The first one? The last one? The one with the lowest value? (enum values can be negative by the way) The one with the highest? The same also applies to range types: MyRange = 3..6.
0 is simply a sane default. It's the task of the programmer to make sure that their code works correctly nevertheless. Not to mention that 0 initialization is something that's very likely to be supported by the operating system: for global variables without explicit value set the BSS section is used which tells the OS to allocate a certain memory area with its content set to 0 (this area contains all unitialized global variables). Same is also true for class instances: it's easy to simply allocate the whole memory area to zero, however to initialize each and every field to a known default is much more costly (see the performance impact that management operators had for managed fields inside classes, before that was improved again).

Regards,
Sven

Reply by email