Recent

Author Topic: fillchar for packed array of bit: strange result  (Read 2616 times)

winni

  • Hero Member
  • *****
  • Posts: 2807
fillchar for packed array of bit: strange result
« 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
« Last Edit: October 11, 2021, 04:30:00 pm by winni »

jamie

  • Hero Member
  • *****
  • Posts: 5051
Re: fillchar for packed array of bit: strange result
« Reply #1 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..
The only true wisdom is knowing you know nothing

Kays

  • Sr. Member
  • ****
  • Posts: 383
  • Whasup!?
    • KaiBurghardt.de
Re: fillchar for packed array of bit: strange result
« Reply #2 on: October 11, 2021, 05:25:02 pm »
Code: Pascal  [Select][+][-]
  1. 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 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.
« Last Edit: October 11, 2021, 05:42:55 pm by Kays »
Yours Sincerely
Kai Burghardt

winni

  • Hero Member
  • *****
  • Posts: 2807
Re: fillchar for packed array of bit: strange result
« Reply #3 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

howardpc

  • Hero Member
  • *****
  • Posts: 3841
Re: fillchar for packed array of bit: strange result
« Reply #4 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.

winni

  • Hero Member
  • *****
  • Posts: 2807
Re: fillchar for packed array of bit: strange result
« Reply #5 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


PascalDragon

  • Hero Member
  • *****
  • Posts: 3525
  • Compiler Developer
Re: fillchar for packed array of bit: strange result
« Reply #6 on: October 12, 2021, 09:22:30 am »
A few notes:

  • packed is ignored for arrays (no matter if dynamic or not)
  • bitpacked (what you probably wanted) is not allowed for dynamic arrays
  • As 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

  • Hero Member
  • *****
  • Posts: 2807
Re: fillchar for packed array of bit: strange result
« Reply #7 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

PascalDragon

  • Hero Member
  • *****
  • Posts: 3525
  • Compiler Developer
Re: fillchar for packed array of bit: strange result
« Reply #8 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

winni

  • Hero Member
  • *****
  • Posts: 2807
Re: fillchar for packed array of bit: strange result
« Reply #9 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

jamie

  • Hero Member
  • *****
  • Posts: 5051
Re: fillchar for packed array of bit: strange result
« Reply #10 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.

The only true wisdom is knowing you know nothing

 

TinyPortal © 2005-2018