Recent

Author Topic: [SOLVED] Function Result is not isolated  (Read 2445 times)

tudi_x

  • Hero Member
  • *****
  • Posts: 532
[SOLVED] Function Result is not isolated
« on: July 18, 2018, 03:26:12 pm »
hi,
as per attached it looks like function Result is not "isolated" maybe due to a Randomize.
it happens on line 83 in unit generate_no, first time invoked result.

is this intended behavior, making Result:= '' mandatory to use?
anyway there is no system warning as per uninitialized variables.
please advise.
thank you

(Linux Mint 19 XFCE, 1.8.4 64b)
« Last Edit: July 18, 2018, 04:34:04 pm by tudi_x »
Lazarus 2.0.2 64b on Debian LXDE 10

Blaazen

  • Hero Member
  • *****
  • Posts: 3237
  • POKE 54296,15
    • Eye-Candy Controls
Re: Function Result is not isolated
« Reply #1 on: July 18, 2018, 03:39:11 pm »
I use Lazarus 1.9.0 r58554M FPC 3.1.1 x86_64-linux-qt and I got warning:
generate_no.pas(83,22) Warning: function result variable of a managed type does not seem to be initialized

Put there
Code: Pascal  [Select][+][-]
  1. Result:='';
in front of the loop.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Function Result is not isolated
« Reply #2 on: July 18, 2018, 03:49:17 pm »
hi,
as per attached it looks like function Result is not "isolated" maybe due to a Randomize.
it happens on line 83 in unit generate_no, first time invoked result.

is this intended behavior, making Result:= '' mandatory to use?
anyway there is no system warning as per uninitialized variables.
please advise.
thank you

(Linux Mint 19 XFCE, 1.8.4 64b)
result is a local variable. local variables are not initialized no. I have no idea what isolated means or what it suppose to do so can't really answer your main question. as for the warning even in lazarus 1.4.4 I got a warning that result is not initialized (see attachment) so you probably missed it in the sea of warning that fpc generates.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

tudi_x

  • Hero Member
  • *****
  • Posts: 532
Re: Function Result is not isolated
« Reply #3 on: July 18, 2018, 04:06:10 pm »
if result is a local variable to a method is it intended behavior to take a value from another local variable with same name from another method (scope).
isn't there a block of memory freshly allocated for the local variables of the new method invoked?

the intended behavior of the app is to have some random IPs and numbers generated.
what can be seen is that the last IP generated appears twice, also concatenated with the first number generated.
there is no append in memo1 for this IP twice.
 
Lazarus 2.0.2 64b on Debian LXDE 10

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Function Result is not isolated
« Reply #4 on: July 18, 2018, 04:19:38 pm »
if result is a local variable to a method is it intended behavior to take a value from another local variable with same name from another method (scope).
isn't there a block of memory freshly allocated for the local variables of the new method invoked?

the intended behavior of the app is to have some random IPs and numbers generated.
what can be seen is that the last IP generated appears twice, also concatenated with the first number generated.
there is no append in memo1 for this IP twice.
one last time. The contents of local variables are not initialized. The fact that the last ip was used twice is simple a statistical probability that the memory manager re used a previously used address, it might be the memory for the previous result or the memory of the data before copied to the result. In a tight loop with similar memory sizes it is more probable to re use the last memory address, taking in to account the caching nature of the existing memory manager.
In short always initialize your local variables including result.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

tudi_x

  • Hero Member
  • *****
  • Posts: 532
Re: Function Result is not isolated
« Reply #5 on: July 18, 2018, 04:33:51 pm »
understood.
thank you!
Lazarus 2.0.2 64b on Debian LXDE 10

Blaazen

  • Hero Member
  • *****
  • Posts: 3237
  • POKE 54296,15
    • Eye-Candy Controls
Re: [SOLVED] Function Result is not isolated
« Reply #6 on: July 18, 2018, 04:42:59 pm »
For curiosity: I tried to compile with -gt (trash local vars) and the output is:
Code: Pascal  [Select][+][-]
  1. ----y----
  2. uninitialized function result in function TGenerateNumber.GenerateRunes(pLength:Byte):AnsiString;6293113621
  3. 66
  4. 661
  5. 136
  6. 6721
  7. 61
  8. 612
  9. 8127
  10. ----z----
  11. 0.68.62.33
  12. 0.16.61.7
  13. 0.161.141.217
  14. 111.111.111.111
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

 

TinyPortal © 2005-2018