Recent

Author Topic: Variable "xyz" doesn't not seem to be initialized (again/still) :(  (Read 2532 times)

Thaddy

  • Hero Member
  • *****
  • Posts: 18524
  • Here stood a man who saw the Elbe and jumped it.
Re: Variable "xyz" doesn't not seem to be initialized (again/still) :(
« Reply #30 on: November 14, 2025, 06:36:20 pm »
@creaothceann

Where are your compilable examples? There are noon thusfar? Nada, nothing? Is that on purpose? Intentional?

Really helping other members?
Shut up and stop this flaming war. You know you are wrong anyway. Even by "declaration", the mark of a dictator.
You can even break "Hello, world"

You can hardly expect me to take you serious.
« Last Edit: November 14, 2025, 06:45:35 pm by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

440bx

  • Hero Member
  • *****
  • Posts: 5896
Re: Variable "xyz" doesn't not seem to be initialized (again/still) :(
« Reply #31 on: November 14, 2025, 06:47:03 pm »
Edsger Dijkstra may have been arrogant but he was absolutely right about OOP.  His arrogance is often cited by OOP proponents as an indication he was wrong which is an obvious logical fallacy.  There is obviously no correlation between the two facts but, it's convenient to pretend there is one for those who don't see his points.
FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

Thaddy

  • Hero Member
  • *****
  • Posts: 18524
  • Here stood a man who saw the Elbe and jumped it.
Re: Variable "xyz" doesn't not seem to be initialized (again/still) :(
« Reply #32 on: November 14, 2025, 06:53:12 pm »
 :o Well, eventually we two always agree: you provide compilable examples....  :-X

Or express real issues in an understandable and documented way.
That makes discussions far more fruitfull. You are very precise.

If I reply to you I feel I am not wasting my time. Right or wrong.
« Last Edit: November 14, 2025, 06:59:18 pm by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

Mr.Madguy

  • Hero Member
  • *****
  • Posts: 880
Re: Variable "xyz" doesn't not seem to be initialized (again/still) :(
« Reply #33 on: November 15, 2025, 06:22:15 am »
Yeah, I have to agree. I also don't like, when some warnings/hints are generated. That's why I always fix problems in my code, that cause them. Problem is - while Delphi and GCC don't generate "exceeding" warnings/hints, FPC does. Common problems: 1) Variable isn't initialized, while function call does exactly this 2) Variable is unused in callback functions.
Is it healthy for project not to have regular stable releases?
Just for fun: Code::Blocks, GCC 13 and DOS - is it possible?

PascalDragon

  • Hero Member
  • *****
  • Posts: 6238
  • Compiler Developer
Re: Variable "xyz" doesn't not seem to be initialized (again/still) :(
« Reply #34 on: November 17, 2025, 10:22:46 pm »
The most common cases are either calls to standard functions like FillChar() or BlockRead() at the beginning of a function/procedure, for the explicit purpose of "initializing" the variable in the parameter of those two functions. Or passing an array/record variable to a self-written procedure like InitializeStructure (MyRecord), likewise for the explicit purpose of initializing the passed variable.

Just use MyVar := Default(TypeOfVar).

In most non-trivial applications, this just might simply not work, as it assumes that this is a local variable that gets initialized "once", and as I mentioned elsewhere already, twice, such a "default" by the compiler might simply not be what the application needs at runtime...

You can assign Default(TypeOfVar) to a variable as often as you want and it's essentially equivalent to a FillChar.

Mr.Madguy

  • Hero Member
  • *****
  • Posts: 880
Re: Variable "xyz" doesn't not seem to be initialized (again/still) :(
« Reply #35 on: November 18, 2025, 07:41:31 am »
You can assign Default(TypeOfVar) to a variable as often as you want and it's essentially equivalent to a FillChar.
Problem is - in some cases it's waste of time. For example when function call fills some structure and it's unused anyway, if function fails. Like in case of API calls.
Is it healthy for project not to have regular stable releases?
Just for fun: Code::Blocks, GCC 13 and DOS - is it possible?

PascalDragon

  • Hero Member
  • *****
  • Posts: 6238
  • Compiler Developer
Re: Variable "xyz" doesn't not seem to be initialized (again/still) :(
« Reply #36 on: November 20, 2025, 10:18:38 pm »
You can assign Default(TypeOfVar) to a variable as often as you want and it's essentially equivalent to a FillChar.
Problem is - in some cases it's waste of time. For example when function call fills some structure and it's unused anyway, if function fails. Like in case of API calls.

Then just put it into the code paths where they're needed, there's no requirement to have the initialization at the start of the function.

ASerge

  • Hero Member
  • *****
  • Posts: 2469
Re: Variable "xyz" doesn't not seem to be initialized (again/still) :(
« Reply #37 on: November 21, 2025, 04:46:45 pm »
I think that in some situations the implementation of the Default function should be improved.
Code: Pascal  [Select][+][-]
  1. {$MODE OBJFPC}
  2.  
  3. type
  4.   TSome = array[0..100000] of Byte;
  5.  
  6. var
  7.   Some: TSome;
  8.  
  9. procedure Test;
  10. begin
  11.   Some := Default(TSome);
  12. end;
  13.  
  14. begin
  15.   Test;
  16. end.

Code: ASM  [Select][+][-]
  1. .section .text.n_p$program_$$_test,"x"
  2.         .balign 16,0x90
  3. .globl  P$PROGRAM_$$_TEST
  4. P$PROGRAM_$$_TEST:
  5. .seh_proc P$PROGRAM_$$_TEST
  6. # [project1.lpr]
  7. # [10] begin
  8.         pushq   %rdi
  9. .seh_pushreg %rdi
  10.         pushq   %rsi
  11. .seh_pushreg %rsi
  12.         movl    $24,%r10d
  13. .Lj5:
  14.         leaq    -4096(%rsp),%rsp
  15.         movl    %eax,(%rsp)
  16.         subq    $1,%r10
  17.         jne     .Lj5
  18.         leaq    -1736(%rsp),%rsp
  19. .seh_stackalloc 100040
  20. .seh_endprologue
  21. # Var $_zero_$P$PROGRAM_$$_TSOME located at rsp+32, size=OS_NO
  22.         leaq    32(%rsp),%rcx
  23.         xorl    %r8d,%r8d
  24.         movl    $100001,%edx
  25.         call    SYSTEM_$$_FILLCHAR$formal$INT64$BYTE
  26. # [11] Some := Default(TSome);
  27.         leaq    U_$P$PROGRAM_$$_SOME(%rip),%rdi
  28.         leaq    32(%rsp),%rsi
  29.         movl    $12500,%ecx
  30.         rep
  31.         movsq
  32.         movsb
  33. # [12] end;
  34.         nop
  35.         leaq    100040(%rsp),%rsp
  36.         popq    %rsi
  37.         popq    %rdi
  38.         ret

Allocates 100,000 bytes on the stack (!), zeroing it, and copies it to a global variable.

440bx

  • Hero Member
  • *****
  • Posts: 5896
Re: Variable "xyz" doesn't not seem to be initialized (again/still) :(
« Reply #38 on: November 21, 2025, 05:24:37 pm »
I think that in some situations the implementation of the Default function should be improved.

Allocates 100,000 bytes on the stack (!), zeroing it, and copies it to a global variable.
That's the reason I stopped using "= ();" in my code to initialize structures, the code it generates is, more often than not, totally dismal.

When I need to initialize a structure, I do it "manually" using ZeroMemory.

It's too bad the generated code is so poor because it is a clear and succinct way of setting the initial value.

FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

 

TinyPortal © 2005-2018