Sign in Join

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

Mailing lists / fpc-pascal / 2014-03

struct definition in C

9 messages · 5 people · –

Reply by email Subscribe to this list Your subscription

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

Darius Blaszyk

Hi,

What would be the equivalent of the following struct definition in C?

typedef struct MyStruct
{
 unsigned a :6;
 unsigned b :15;
 unsigned c:11;
}MyStruct;

I imagine this is some sort of struct initialization. Is thi spossible in FPC as well, or what is the best alternative?

Kind regards, Darius Blaszyk

Reply by email

Jonas Maebe
to Darius Blaszyk
Darius Blaszyk wrote 11 lines
What would be the equivalent of the following struct definition in C? typedef struct MyStruct { unsigned a :6; unsigned b :15; unsigned c:11; }MyStruct; I imagine this is some sort of struct initialization. Is thi spossible in FPC as well, or what is the best alternative?

It's not an initialization, it's a struct with bitfields. FPC currently does not support C-compatible bitfield packing. To get a record with equal alignment and size you can declare it as "record data: cuint; end;", but there is currently no way in FPC to access the bitfields in a portable way across platforms.

Jonas

Reply by email

Bernd Oppolzer
to Jonas Maebe
Jonas Maebe wrote 23 lines
On 23/03/14 19:39, Darius Blaszyk wrote: > What would be the equivalent of the following struct definition in C? > > typedef struct MyStruct > { > unsigned a :6; > unsigned b :15; > unsigned c:11; > }MyStruct; > > I imagine this is some sort of struct initialization. Is thi spossible > in FPC as well, or what is the best alternative? It's not an initialization, it's a struct with bitfields. FPC currently does not support C-compatible bitfield packing. To get a record with equal alignment and size you can declare it as "record data: cuint; end;", but there is currently no way in FPC to access the bitfields in a portable way across platforms. Jonas
I would like to add that bitfields in C are not at all portable;
it is completely implementation dependant if they are
allocated from right to left in the structure or the other way round etc.;

I do lot of portable development in C, and I never use bitfields for that reason;
if I really need to store information in single bits, I use ints or chars and access the individual bits using logical operations. Of course, the problem with big-endian / little-endian has to be taken into account, too
(not, if you use unsigned chars as the base for the bit strings, and if the chars are
eight bits long, which should be the case on all modern architectures).

Kind regards

Bernd

Reply by email

Luca Olivetti
to Darius Blaszyk: What would be the equivalent of the following struct definition in C? typedef struct…

El 23/03/14 19:39, Darius Blaszyk ha escrit:

Show quoted text 13 lines
Hi, What would be the equivalent of the following struct definition in C? typedef struct MyStruct { unsigned a :6; unsigned b :15; unsigned c:11; }MyStruct; I imagine this is some sort of struct initialization. Is thi spossible in FPC as well, or what is the best alternative?

h2pas translates it to a record with associated setters/getters for each field.

You could also try with bitpacked records, i.e.

type MyStruct = bitpacked record
    a:0..(1 shl 6)-1;
    b:0..(1 shl 15)-1;
    c:0..(1 shl 11)-1;
end;

but, as Jonas said, it't not guaranteed to work (in practice it works with win32/linux 32 and linux 64, though it could break with a different version of the compiler).

Bye

Luca

Reply by email

Darius Blaszyk
to Jonas Maebe: It's not an initialization, it's a struct with bitfields. FPC currently does not support…

Thanks Jonas!
One more question. Why is it that there is no portable way to access the bitfields? Is that because of endian issues, or is there more to it? What about declaring the bitfields in a bitpacked record as "array of boolean"?

Regards, Darius

Jonas Maebe schreef op 23 mrt '14:

Show quoted text 15 lines
On 23/03/14 19:39, Darius Blaszyk wrote: > What would be the equivalent of the following struct definition in C? typedef struct MyStruct { unsigned a :6; unsigned b :15; unsigned c:11; }MyStruct; I imagine this is some sort of struct initialization. Is thi spossible in FPC as well, or what is the best alternative? It's not an initialization, it's a struct with bitfields. FPC currently does not support C-compatible bitfield packing. To get a record with equal alignment and size you can declare it as "record data: cuint; end;", but there is currently no way in FPC to access the bitfields in a portable way across platforms. Jonas _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal [1]

Links:
------
[1] http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Reply by email

Jonas Maebe
to Darius Blaszyk
Darius Blaszyk wrote 10 lines
Jonas Maebe schreef op 23 mrt '14: > It's not an initialization, it's a struct with bitfields. FPC currently > does not support C-compatible bitfield packing. To get a record with > equal alignment and size you can declare it as "record data: cuint; > end;", but there is currently no way in FPC to access the bitfields in a > portable way across platforms. > One more question. Why is it that there is no portable way to access the bitfields?

In part it's because no one has been able yet to come up with a natural way to specify them in Pascal. Note that the C declaration both has a base type (the "unsigned") and a bit width (the :x). Both pieces of information are required to determine the layout of the struct and to calculate its alignment. In part it's also because even with all that information, it's quite complex and sometimes ABI-dependant to generate a correct layout.

Is that because of endian issues, or is there more to it? What about declaring the bitfields in a bitpacked record as "array of boolean"?

That will not result in the proper alignment of the struct, and depending on how the struct is defined in C it may also not result in mapping the bits to the correct locations. "bitpacked" only matches the C behaviour if the struct has been declared with __attribute__((packed)).

Jonas

Reply by email

Michael Schnell
to Bernd Oppolzer: I would like to add that bitfields in C are not at all portable; it is completely…
Bernd Oppolzer wrote 5 lines
I would like to add that bitfields in C are not at all portable; it is completely implementation dependant if they are allocated from right to left in the structure or the other way round etc.;

I _found_ that in bitfield records, with high endian archs gcc starts with filling the high bits, while with low endian archs it starts with filling the lower bits in the basic type that is given. IMHO this in fact does make sense.

I did not find a documentation about this and have no idea whether it is likely that other compilers work similar.

-Michael

Reply by email