Bit Field Alignment

B

bg

Hi,

I would like to know how memory will be aligned for the following
structure containing bit field.

struct bitfld
{
char a: 2;
int b: 2 ;
char c: 2;
} foo;

where char is 1 byte and int is 2 byte data types and 1 byte packing is
used.

I would like to know whether memor will be aligned as

1. Allocate 2 bits for a. Then leave 6 bits(Since next is an int type
bit field).
Allocate 2 bits for b. Then leave 6 bits(Since next is a char type
bit field).
Allocate 2 bits for c.
So altogether 3 bytes will be allocated for structure foo.

OR as

2. Allocate 2 bits for a, 2 bits for b and 2 bits for c. All in the
same byte.
So altogether 1 byte will be allocated for structure foo.

Hoping your reply at the earliest,
Byju
 
K

Keith Thompson

bg said:
I would like to know how memory will be aligned for the following
structure containing bit field.

struct bitfld
{
char a: 2;
int b: 2 ;
char c: 2;
} foo;

where char is 1 byte and int is 2 byte data types and 1 byte packing is
used.

Any way the compiler likes -- assuming it supports bit fields of type
char.

The only portable types for bit fields are int, unsigned int, signed
int, and (C99 only) _Bool. Plain int may be treated as either signed
or unsigned (this applies only to bit fields). (Bit fields of other
integer types are a common extension, though.)
 
?

=?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=

bg said:
Hi,

I would like to know how memory will be aligned for the following
structure containing bit field.

struct bitfld
{
char a: 2;
int b: 2 ;
char c: 2;
} foo;

where char is 1 byte and int is 2 byte data types and 1 byte packing is
used.

I would like to know whether memor will be aligned as

1. Allocate 2 bits for a. Then leave 6 bits(Since next is an int type
bit field).
Allocate 2 bits for b. Then leave 6 bits(Since next is a char type
bit field).
Allocate 2 bits for c.
So altogether 3 bytes will be allocated for structure foo.

OR as

2. Allocate 2 bits for a, 2 bits for b and 2 bits for c. All in the
same byte.
So altogether 1 byte will be allocated for structure foo.

Hoping your reply at the earliest,

Well, C doesn't specify packed structs, particular alignment
requirements for your particular target, or order of the bitfields.

Check your compiler documentation, or perhaps just make a simple test
case if you can.
 
B

bg

can u specify how memory will be aligned if the structure was

struct bitfld
{
int a: 2;
long int b: 2 ;
int c: 2;
} foo;

where 'int' is 1 byte and 'long int' is 2 byte data types and 1 byte
packing is used
 
V

Vladimir S. Oka

bg opined:
can u specify how memory will be aligned if the structure was

struct bitfld
{
int a: 2;
long int b: 2 ;
int c: 2;
} foo;

where 'int' is 1 byte and 'long int' is 2 byte data types and 1 byte
packing is used

To quote from Keith's original reply, which you didn't quote, which is
a Bad Thing:

Keith said:
Any way the compiler likes

Keith's comment about `char` fields had nothing to do with alignment,
just portability as fields of that type may not be supported by all
compilers. Relying on particular alignment will make your code
non-portable in any case (even with all `int` fields as above). This
is not to say that your application must not use such knowledge (you
may be memory mapping I/O ports or something). It's just that you
should be aware that things may change if you change the compiler or
anything else about your implementation/architecture.

If you really need to know, your compiler documentation may tell you
how exactly how bit fields are aligned. Look there, or ask in a group
discussing your tool chain.

Last, but not the least, please quote context, and read the link in my
sig.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top