473,395 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Bit Field Alignment

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
4 3877
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
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: jlemay | last post by:
if you look at http://www.emrose.net/contact1.html the last field is a textarea field and I can't make it align with the other text fields. it's pretty close, but this comes from trial and...
11
by: Bradford Chamberlain | last post by:
I work a lot with multidimensional arrays of dynamic size in C, implementing them using a single-dimensional C array and the appropriate multipliers and offsets to index into it appropriately. I...
4
by: Shashi | last post by:
Can somebody explain how the byte alignment for structures work, taking the following example and considering: byte of 1 Byte word of 2 Bytes dword of 4 Bytes typedef struct { byte a; word...
67
by: S.Tobias | last post by:
I would like to check if I understand the following excerpt correctly: 6.2.5#26 (Types): All pointers to structure types shall have the same representation and alignment requirements as each...
13
by: aegis | last post by:
The following was mentioned by Eric Sosman from http://groups.google.com/group/comp.lang.c/msg/b696b28f59b9dac4?dmode=source "The alignment requirement for any type T must be a divisor of...
4
by: MC | last post by:
In C, printf allows us to specify a runtime (non-constant) field width of a formatted value by using the * character. Is there something like that for System.String.Format? In C#, I find myself...
12
by: Yevgen Muntyan | last post by:
Hey, Consider the following code: #include <stdlib.h> #define MAGIC_NUMBER 64 void *my_malloc (size_t n) { char *result = malloc (n + MAGIC_NUMBER);
6
by: marktxx | last post by:
Although the C90 standard only mentions the use of 'signed int' and 'unsigned int' for bit-fields (use 'int' at your own risk) and C99 adds _Bool. It seems that most compilers create the size of...
1
DebadattaMishra
by: DebadattaMishra | last post by:
Introduction In case of rich applications, you must have observed that a text field behaves like a dynamic combo box. When the user enters some characters in the text field, a popup will come up...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.