Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2010-07

sub byte arrays

7 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

theo

This is probably a stupid question, but I'll ask anyway ;-)

Is there any support from the compiler to create arrays of, say

type ttest=(one,two,three,four);

Afair, these states could be represented with 2 bits. 00,01,10,11

So four of these items could be represented in a byte (in an array of ttest).

I know that these enumerations take 4 bytes by default, and this can be reduced with {$Z1}

My queston: is there any support from the compiler to pack 4 ttest in 4 array items (using 1 byte) or do i have to fiddle with bit-shift operations on a byte type?

Does {$BITPACKING ON} have to do with it?

Thanks and sorry.

Theo

Jonas Maebe
to theo
theo wrote 8 lines
Is there any support from the compiler to create arrays of, say type ttest=(one,two,three,four); Afair, these states could be represented with 2 bits. 00,01,10,11 So four of these items could be represented in a byte (in an array of ttest).

Declare the array as "bitpacked":

type
  ttestarray = bitpacked array[0..3] of ttest;

Note that dynamic arrays cannot be bitpacked.

Jonas

theo

Thank you Jonas.

Now I was trying if something like this would work:

TTest=(one,two,three,four);
TTestarray = {bitpacked} array [1..100000] of ttest;
PTestarray = ^TTestarray;
var lMask: PTestarray;
begin
getmem(lMask, 25);
 lMask^[99]:=three;
 case lMask^[99] of
  one:writeln('one');
  two:writeln('two');
  three:writeln('three');
  four:writeln('four');
 end;
 freemem(lMask);
end;

It seems, to work, but it even works with commented "bitpacked". Shouldn't this fail? How can I test this? I have all checks on which Lazarus IDE offers, but it's still happy.

From what I understand, this would need 400 bytes in this scenario (not

bitpacked), isn't it?

Thank you
Theo

Jonas Maebe
to theo
theo wrote 9 lines
Now I was trying if something like this would work: TTest=(one,two,three,four); TTestarray = {bitpacked} array [1..100000] of ttest; PTestarray = ^TTestarray; var lMask: PTestarray; begin getmem(lMask, 25);

Uses new(lMask) instead, then the compiler will automatically allocate the right size for you.

Show quoted text 13 lines
lMask^[99]:=three; case lMask^[99] of one:writeln('one'); two:writeln('two'); three:writeln('three'); four:writeln('four'); end; freemem(lMask); end; It seems, to work, but it even works with commented "bitpacked". Shouldn't this fail?

You are writing into unallocated memory. The result of such an operation is "undefined". That means that it can to work, can crash, or anything in between.

How can I test this? I have all checks on which Lazarus IDE offers, but it's still happy.

Have you enabled heaptrc? (-gh)

Jonas

José Mejuto
to theo: Thank you Jonas. Now I was trying if something like this would work:…

Hello theo,

Tuesday, July 20, 2010, 12:03:28 PM, you wrote:

t> Now I was trying if something like this would work: t> TTest=(one,two,three,four);
t> TTestarray = {bitpacked} array [1..100000] of ttest; t> PTestarray = ^TTestarray;
[...]
t> It seems, to work, but it even works with commented "bitpacked". t> Shouldn't this fail? How can I test this? I have all checks on which t> Lazarus IDE offers, but it's still happy.

>From what I understand, this would need 400 bytes in this scenario (not

t> bitpacked), isn't it?

Had you tried SizeOf(TTestarray) ?

Best regards, José
theo
José Mejuto wrote
Had you tried SizeOf(TTestarray) ?

Good idea. Now I can see that the theory is right. for: TTestarray = array [1..100] of ttest;
It goes down from 400 to 25 (using bitpacked).

Jonas Maebe wrote

Uses new(lMask) instead, then the compiler will automatically allocate

the right size for you.

I don't think this is going to solve my problem.
I need to define the
getmem(lMask, (Elements div 4) +1);
this should be possible, right?

Thank you both.
Theo

Alexander Klenin
to theo
theo wrote 17 lines
José Mejuto wrote: > Had you tried SizeOf(TTestarray) ? Good idea. Now I can see that the theory is right. for: TTestarray = array [1..100] of ttest; It goes down from 400 to 25 (using bitpacked). Jonas Maebe wrote > Uses new(lMask) instead, then the compiler will automatically allocate the right size for you. I don't think this is going to solve my problem. I need to define the size at runtime. With getmem(lMask, (Elements div 4) +1); this should be possible, right?

BTW, I think that bitpacked should be extended to support dynamic arrays -- it would be useful for bit vectors, for example.

Alexander S. Klenin