fillchar for packed array of bit: strange result lazarus / 62-General ---------------------------------------------------------------------- winni · 2021-10-11 14:27 Hi! Given is Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i TYPETBit = 0..1;TBigSet =   packed array of TBit;       VAR BS  : TBigSet;....SetLength(BS,256);writeln (sizeOf (BS))  The result is 8 - as exspected. But now it's getting strange: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i fillchar(BS[0],sizeof(BS),1);  This fills 8 bits and not 8 byte as it should! Is this a bug or am I thinking wrong? I am curious. Winni ---------------------------------------------------------------------- jamie · 2021-10-11 14:44 Shouldn't you be using LENGTH and not SizeOf which is a compile time object size, more like a pointer to the object.. if you did this in 64bit then I guess it would explain a little. Just my observations.. ---------------------------------------------------------------------- Kays · 2021-10-11 15:25 Quote from: winni on October 11, 2021, 04:27:37 pmCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i fillchar(BS[0],sizeof(BS),1);BS is a dynamic array. sizeOf a dynamic array variable returns the size of a pointer, not the dynamically allocated array data. You actually wanted to writeCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i fillChar(BS[0], length(BS) * sizeOf(BS[0]), 1);but then I am wondering why you aren’t simply using a for loop and a plain := assignment. ---------------------------------------------------------------------- winni · 2021-10-11 15:56 Thanx Kays! II did not know the pointer stuff. Now it's clear. Quote from: Kays on October 11, 2021, 05:25:02 pmbut then I am wondering why you aren’t simply using a for loop and a plain := assignment. Because I use fillchar since UCSD times to speed up initialization ..... btw.: Are elements of a  dynamic array initialized with zeros or not? Hard to find an answer in the docs. Winni ---------------------------------------------------------------------- howardpc · 2021-10-11 16:02 Quote from: winni on October 11, 2021, 05:56:38 pmbtw.: Are elements of a  dynamic array initialized with zeros or not? Hard to find an answer in the docs. AFAIK all such compiler-managed types are initialised with zeros. ---------------------------------------------------------------------- winni · 2021-10-11 16:46 Quote from: howardpc on October 11, 2021, 06:02:50 pmQuote from: winni on October 11, 2021, 05:56:38 pmbtw.: Are elements of a  dynamic array initialized with zeros or not? Hard to find an answer in the docs. AFAIK all such compiler-managed types are initialised with zeros. Thanx. Confirmed with test: For i := 1 to 1 000 000 do * setLength(BS,1024*1024); * check if only zeros * fill with ones * setLength to zero Not one error. It seems that the initialization fills with zeros. Winni Set ---------------------------------------------------------------------- PascalDragon · 2021-10-12 07:22 A few notes: packed is ignored for arrays (no matter if dynamic or not)bitpacked (what you probably wanted) is not allowed for dynamic arraysAs Kays wrote, SizeOf returns the size of the pointer to the dynamic array while Length returns the number of elements In essence the data of your TBigSet will always be a multiple of a Byte. For really accessing bits in the array you need to do this manually using bit operators. ---------------------------------------------------------------------- winni · 2021-10-12 19:31 Quote from: PascalDragon on October 12, 2021, 09:22:30 amA few notes: ... bitpacked (what you probably wanted) is not allowed for dynamic arrays... In essence the data of your TBigSet will always be a multiple of a Byte. For really accessing bits in the array you need to do this manually using bit operators. Hi Thanx  for this info! So you have to fool the compiler: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i TYPE   TBit = 0..1;   Tpack = Record case boolean of            true : (D: Dword);            false: (b: bitPacked array[0..31] of TBit);          end;   TBigset = Array of Tpack;   ....  procedure conv (v: DWord; var d: dword; var b : byte); inline;  var tmp: dword;  begin  divmod(v, 32,d,tmp);  b := tmp;  end;     procedure ChangeBit (var BS : TBigset; bit: dword; BitVal : TBit);  var d: dword;      b : byte;  begin   conv(bit,d,b);   BS[d].b[b] := BitVal;   end;     A little bit more complicated but it works. Winni ---------------------------------------------------------------------- PascalDragon · 2021-10-13 07:41 Quote from: winni on October 12, 2021, 09:31:25 pmSo you have to fool the compiler: I wouldn't say that this is fooling the compiler as you're simply using language features the way they're supposed to be used... ---------------------------------------------------------------------- winni · 2021-10-13 08:37 Quote from: PascalDragon on October 13, 2021, 09:41:23 am I wouldn't say that this is fooling the compiler as you're simply using language features the way they're supposed to be used...  Hi I just wanted to remark that it is not very comfortable that bitpacked is not allowed for dynamic arrays. Winni ---------------------------------------------------------------------- jamie · 2021-10-13 14:16 I believe that is what Tbits class is for. Although I find it a little overboard with how it's coded. I use something of my own instead.