Lazarus

Free Pascal => General => Topic started by: winni on October 11, 2021, 04:27:37 pm

Title: fillchar for packed array of bit: strange result
Post by: winni on October 11, 2021, 04:27:37 pm
Hi!

Given is

Code: Pascal  [Select][+][-]
  1. TYPE
  2. TBit = 0..1;
  3. TBigSet =   packed array of TBit;      
  4.  
  5. VAR
  6. BS  : TBigSet;
  7. ....
  8. SetLength(BS,256);
  9. writeln (sizeOf (BS))
  10.  

The result is 8 - as exspected.

But now it's getting strange:

Code: Pascal  [Select][+][-]
  1. fillchar(BS[0],sizeof(BS),1);
  2.  

This fills 8 bits and not 8 byte as it should!

Is this a bug or am I thinking wrong?

I am curious.

Winni
Title: Re: fillchar for packed array of bit: strange result
Post by: jamie on October 11, 2021, 04:44:04 pm
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..
Title: Re: fillchar for packed array of bit: strange result
Post by: Kays on October 11, 2021, 05:25:02 pm
Code: Pascal  [Select][+][-]
  1. fillchar(BS[0],sizeof(BS),1);
BS is a dynamic array. sizeOf (https://wiki.freepascal.org/Dynamic_array#handling) a dynamic array variable returns the size of a pointer, not the dynamically allocated array data. You actually wanted to write
Code: Pascal  [Select][+][-]
  1. 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.
Title: Re: fillchar for packed array of bit: strange result
Post by: winni on October 11, 2021, 05:56:38 pm
Thanx Kays!

II did not know the pointer stuff. Now it's clear.


but 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
Title: Re: fillchar for packed array of bit: strange result
Post by: howardpc on October 11, 2021, 06:02:50 pm
btw.: 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.
Title: Re: fillchar for packed array of bit: strange result
Post by: winni on October 11, 2021, 06:46:15 pm
btw.: 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

Title: Re: fillchar for packed array of bit: strange result
Post by: PascalDragon on October 12, 2021, 09:22:30 am
A few notes:


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.
Title: Re: fillchar for packed array of bit: strange result
Post by: winni on October 12, 2021, 09:31:25 pm
A 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][+][-]
  1. TYPE
  2.    TBit = 0..1;
  3.    Tpack = Record case boolean of
  4.             true : (D: Dword);
  5.             false: (b: bitPacked array[0..31] of TBit);
  6.             end;
  7.    TBigset = Array of Tpack;
  8.    ....
  9.   procedure conv (v: DWord; var d: dword; var b : byte); inline;
  10.   var tmp: dword;
  11.   begin
  12.   divmod(v, 32,d,tmp);
  13.   b := tmp;
  14.   end;
  15.  
  16.  
  17.   procedure ChangeBit (var BS : TBigset; bit: dword; BitVal : TBit);
  18.   var d: dword;
  19.       b : byte;
  20.   begin
  21.    conv(bit,d,b);
  22.    BS[d].b[b] := BitVal;
  23.    end;
  24.  
  25.  
  26.  
   

A little bit more complicated but it works.

Winni
Title: Re: fillchar for packed array of bit: strange result
Post by: PascalDragon on October 13, 2021, 09:41:23 am
So 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...  :-X
Title: Re: fillchar for packed array of bit: strange result
Post by: winni on October 13, 2021, 10:37:35 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...  :-X

Hi

I just wanted to remark that it is not very comfortable that bitpacked is not allowed for dynamic arrays.

Winni
Title: Re: fillchar for packed array of bit: strange result
Post by: jamie on October 13, 2021, 04:16:35 pm
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.

TinyPortal © 2005-2018