Ugochukwu Mmaduekwe 42 Posted December 22, 2018 Hello all, I encountered a weird behaviour of FillChar today. The Demo below illustrates the bug. program Project1; {$APPTYPE CONSOLE} {$R *.res} uses SysUtils; procedure PrintArray(const ar: array of UInt32); var i: Int32; begin for i := Low(ar) to High(ar) do begin WriteLn(ar[i]); end; end; var a: array [0 .. 5] of UInt32; begin try { TODO -oUser -cConsole Main : Insert code here } FillChar(a, SizeOf(a), UInt32(4)); PrintArray(a); ReadLn; except on E: Exception do WriteLn(E.ClassName, ': ', E.Message); end; end. I expected Quote 4 4 4 4 4 4 but got the strange values in the image below. Any idea what's wrong? Share this post Link to post
Uwe Raabe 2057 Posted December 22, 2018 FillChar is a byte oriented method it will fill the underlying memory with the given number of bytes of the given value. Check Sizeof(a) and you will see that it is 24 and not 6. The UINT32 cast has no effect as FillChar will only use the lowest byte of that parameter. 2 Share this post Link to post
Guest Posted December 22, 2018 FillChar expects a byte value as the last paramater. You're filling a memory block with byte value 4 and your results show it (67372036 decimal is $04040404). Share this post Link to post
Ugochukwu Mmaduekwe 42 Posted December 22, 2018 (edited) 24 minutes ago, Uwe Raabe said: FillChar is a byte oriented method it will fill the underlying memory with the given number of bytes of the given value. Check Sizeof(a) and you will see that it is 24 and not 6. The UINT32 cast has no effect as FillChar will only use the lowest byte of that parameter. Oh I see, Thanks for replying. Is it possible for me to use FillChar on arrays of UInt32 properly? Let's say an equivalent of FillChar that works with array of UInt32 other than using a loop. Edited December 22, 2018 by Ugochukwu Mmaduekwe Share this post Link to post
Ugochukwu Mmaduekwe 42 Posted December 22, 2018 11 minutes ago, Ondrej Kelle said: FillChar expects a byte value as the last paramater. You're filling a memory block with byte value 4 and your results show it (67372036 decimal is $04040404). Thanks for the explanation. Share this post Link to post
Uwe Raabe 2057 Posted December 22, 2018 12 minutes ago, Ugochukwu Mmaduekwe said: Is it possible for me to use FillChar on arrays of UInt32 properly? In general, no! Unless, in very specific scenarios, when the number you want to fill the array with consists of four similar bytes (like $04040404). 1 Share this post Link to post
Ugochukwu Mmaduekwe 42 Posted December 22, 2018 (edited) 4 minutes ago, Uwe Raabe said: In general, no! Unless, in very specific scenarios, when the number you want to fill the array with consists of four similar bytes (like $04040404). Well thanks for the answer, I guess I will resort to using a for loop for what I want to achieve. Edited December 22, 2018 by Ugochukwu Mmaduekwe Share this post Link to post
Kryvich 165 Posted December 22, 2018 https://stackoverflow.com/questions/2113182/fillchar-but-for-integer-cardinal 1 Share this post Link to post
stijnsanders 35 Posted December 22, 2018 Haha! That's a question of mine! I wonder if it's still the same asm in 64-bits. Share this post Link to post
Rudy Velthuis 91 Posted January 3, 2019 On 12/22/2018 at 2:03 PM, Ugochukwu Mmaduekwe said: but got the strange values in the image below. Any idea what's wrong? Yes. Check that 67372036 is the same as hex $04040404, in other words, 4 bytes, each with value 4. FillChar fills bytes, even if you pass a UInt32 or even a UInt64. It will only take the low byte and multiplicate that. 1 1 Share this post Link to post