By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,963 Members | 1,825 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,963 IT Pros & Developers. It's quick & easy.

Bit Field Alignment

P: n/a
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

Apr 17 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
bg wrote:
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.
Apr 17 '06 #2

P: n/a
"bg" <by********@gmail.com> writes:
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.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Apr 17 '06 #3

P: n/a
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

Apr 17 '06 #4

P: n/a
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 Thompson wrote: 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.

--
"Whip me. Beat me. Make me maintain AIX."
(By Stephan Zielinski)

<http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>

Apr 18 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.