Recent

Author Topic: [SOLVED] Function FillDWord() produces Fatal Segmentation Fault  (Read 1704 times)

domibay_hugo

  • New Member
  • *
  • Posts: 37
  • Site Reliabilty / DevOps Engineer at Domibay S.L.
    • GitHub Profile
[SOLVED] Function FillDWord() produces Fatal Segmentation Fault
« on: November 09, 2020, 01:03:57 pm »
In the intent to swiftly assigning a Value to an Array I tried to use the FillDWord() Function.
When I initialize it with FillByte() it is correctly initialized. But when I want to assign the actual Value with FillDWord() it produces an unrecoverable Crash.

To demonstrate this Behaviour I extended the Example104 from the Official Documentation at:
FillDWord() Documentation
Code: Pascal  [Select][+][-]
  1. Program demofilldword;
  2.  
  3. uses
  4.   SysUtils;
  5.  
  6. { Program to demonstrate the FillDWord function. }
  7.  
  8. Const
  9.   ArraySize = 256;
  10.  
  11. Var
  12.   S : Array [0..ArraySize - 1] of Int64;
  13.   v: Cardinal;
  14.   I : longint;
  15.  
  16. begin
  17.   v := 7;
  18.   S[0] := 0;
  19.  
  20.   try
  21.     WriteLn('Array S - FillByte(): do ...');
  22.  
  23.     FillByte(S,SizeOf(S),0);
  24.  
  25.     For I:=1 to ArraySize - 1 do
  26.       If S[i]<>0 then
  27.         WriteLn('Position ',i,' not zeroed out');
  28.  
  29.     WriteLn('Array S : Init done.');
  30.  
  31.     WriteLn('Array S - FillDWord(): do ...');
  32.  
  33.     FillDWord(S,SizeOf(S),v);
  34.  
  35.     WriteLn('Array S - FillDWord(): Execution done.');
  36.  
  37.     For I:=1 to ArraySize do
  38.       WriteLn('Position ',i,' has the value: '#39, S[I], #39);
  39.  
  40.     WriteLn('Array S - FillDWord(): Check done.');
  41.  
  42.   except
  43.     on e: Exception do WriteLn('Exception [', e.HelpContext, ']: '#39, e.Message, #39);
  44.   end;
  45.  
  46.   WriteLn('Demo App done.');
  47. end.                              
  48.  

This little Prototype produces this Output:

$ ./demofilldword
Array S - FillByte(): do ...
Array S : Init done.
Array S - FillDWord(): do ...
Segmentation fault (core dumped)

(I don't know where the Core Dump is written to but I can't find it)

The actual Value in this Use Case in v is actually "7" so I could use FillByte(), too, but that wouldn't cover other Use Cases for Values > 255 .

Please, can anyone give Insight about this Behaviour ?
« Last Edit: November 09, 2020, 02:07:54 pm by domibay_hugo »

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11452
  • FPC developer.
Re: Function FillDWord() produces Fatal Segmentation Fault
« Reply #1 on: November 09, 2020, 01:09:42 pm »
You get the size of bytes, and then  use store that amount of dwords?!?!

divide by count to fill by sizeof(dword)

domibay_hugo

  • New Member
  • *
  • Posts: 37
  • Site Reliabilty / DevOps Engineer at Domibay S.L.
    • GitHub Profile
Re: Function FillDWord() produces Fatal Segmentation Fault
« Reply #2 on: November 09, 2020, 01:45:11 pm »
Yes, I was actually misusing SizeOf().
I replaced it by Length() or by the  ArraySize Constant.
Code: Pascal  [Select][+][-]
  1.   WriteLn('Array S - SizeOf(): '#39, SizeOf(S), #39);
  2.   WriteLn('Array S - SizeOf(DWord): '#39, SizeOf(DWord), #39);
  3.   WriteLn('Array S - SizeOf()/SizeOf(DWord): '#39, (SizeOf(S) / SizeOf(DWord)), #39);
  4.  

Which produces:

Array S - SizeOf(): '2048'
Array S - SizeOf(DWord): '4'
Array S - SizeOf()/SizeOf(DWord): ' 5.1200000000000000E+002'


I found the Value of 2048 very suspicious so I set ArraySize = 100
Code: Pascal  [Select][+][-]
  1. Const
  2.   ArraySize = 100;
  3.  
Now I get:

Array S - SizeOf(): '800'
Array S - SizeOf(DWord): '4'
Array S - SizeOf()/SizeOf(DWord): ' 2.0000000000000000E+002'


Now the Application does not crash anymore.

But still the Function FillDWord() does not fill the Array correctly
Code: Pascal  [Select][+][-]
  1.     FillDWord(S, ArraySize, DWord(v));
  2.  

Array S - FillDWord(): do ...
Array S - FillDWord(): Execution done.
Position 0 has the value: '30064771079'
Position 1 has the value: '30064771079'
Position 2 has the value: '30064771079'
# [...]
Position 47 has the value: '30064771079'
Position 48 has the value: '30064771079'
Position 49 has the value: '30064771079'
Position 50 has the value: '0'
Position 51 has the value: '0'
Position 52 has the value: '0'
# [...]
Position 94 has the value: '0'
Position 95 has the value: '0'
Position 96 has the value: '0'
Position 97 has the value: '0'
Position 98 has the value: '0'
Position 99 has the value: '0'
Array S - FillDWord(): Check done.
Demo App done.
Heap dump by heaptrc unit of /usr15/proyectos/pascal/strings/demofilldword
6 memory blocks allocated : 719/728
6 memory blocks freed     : 719/728
0 unfreed memory blocks : 0
True heap size : 327680
True free heap : 327680


It is only filled half the way and not with the correct value.
« Last Edit: November 09, 2020, 01:50:34 pm by domibay_hugo »

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11452
  • FPC developer.
Re: Function FillDWord() produces Fatal Segmentation Fault
« Reply #3 on: November 09, 2020, 01:52:09 pm »
your array is int64 (8-byte) while your fill values are 32-bit. (4-byte)

domibay_hugo

  • New Member
  • *
  • Posts: 37
  • Site Reliabilty / DevOps Engineer at Domibay S.L.
    • GitHub Profile
Re: Function FillDWord() produces Fatal Segmentation Fault
« Reply #4 on: November 09, 2020, 02:06:39 pm »
Yes, you are right!

The actually Value in this Use Case is low and always positive, so I expected it to cover the positive range of Int64

When I changed the Array " S " to "Array of Cardinal" FillDWord() filled in the correct value.
Code: Pascal  [Select][+][-]
  1. Var
  2.   S : Array [0..ArraySize - 1] of Cardinal;
  3.  

But in my Use Case I actually need Int64 values.
So, I found that I must use the FillQWord() Function which works also with 8 Byte values.
Code: Pascal  [Select][+][-]
  1.     FillQWord(S, ArraySize, v);
  2.  

Thank you very much for your constructive hints.

 

TinyPortal © 2005-2018