473,394 Members | 2,031 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,394 software developers and data experts.

sizes of bit fields

In ANSI C, we can have a bit fields inside structure like

struct _A {
int i:2;
};

would define a variable of 2-bit length. My doubt is that what would
the size of this structure then? Would it be sizeof(int) or just 1
byte? I know that it is quite compiler specific, but what it should be
according to the ANSI standard? I couldn't find exact documentation
explaining this.

Microsoft compiler does give sizeof(int) for such structure.

Regards,
-Abhishikt

Nov 15 '05 #1
10 3028
Abhi wrote:
would define a variable of 2-bit length. My doubt is that what would
the size of this structure then? Would it be sizeof(int) or just 1
byte?


The actual size of the struct depends on your compiler, and which amount
of space it allocates for each word of data. In gcc, it allocates space
in chunks of 4 bytes (if you exceed 32 bits, it allocates a new one). So

actual_size = size + word_size - size % word_size

This equation does not take in account the padding fields (fields which
bit size is 0, used so as to move next field to the next word boundary).

Regards,
David.
--
David Lago <dl***@mundo-r.com>

PGP key available at: http://pgp.mit.edu
Personal Blizog: http://arcanelinux.org/blog/dlworld
The Enflame Project: http://enflame.org
Nov 15 '05 #2
The equation you have provided assists my understanding. Just a note
that, the word_size is not always in block of 4. It depends on what
data type we use (either int or char).

typedef struct {
int a:7;
int a1:1;
} AA;

typedef struct {
char b:7;
char b1:1;
} BB;

in above two structures AA has sizeof 4 bytes, while BB has 1 byte.

and another point is that we CANNOT have a variable like "char c:9" it
generates a compiler error. (at least on Microsoft compiler).

Regards,
-Abhishikt

Nov 15 '05 #3
On Tue, 05 Jul 2005 03:03:52 -0700, Abhi wrote:
In ANSI C, we can have a bit fields inside structure like

struct _A {
int i:2;
};

would define a variable of 2-bit length. My doubt is that what would
the size of this structure then? Would it be sizeof(int) or just 1
byte? I know that it is quite compiler specific, but what it should be
according to the ANSI standard? I couldn't find exact documentation
explaining this.
The standard leaves the allocation unit for bit-fields up to the
implementation, which is why it is compiler specific.

There is little value in putting a single bit-field in a structure (except
possibly for specific range issues), the value comes when multiple
bit-fields are packed together.
Microsoft compiler does give sizeof(int) for such structure.


Which is one reasonable choice.

Lawrence
Nov 15 '05 #4
Abhi wrote:
The equation you have provided assists my understanding. Just a note
that, the word_size is not always in block of 4. It depends on what
data type we use (either int or char).


Extracted from "The ANSI C Programming Language, 2nd Ed." by Kernighan &
Ritchie:

[...] Fields may be declared only as ints; for portability, specify
signed or unsigned explicitly.

David.
--
David Lago <dl***@mundo-r.com>

PGP key available at: http://pgp.mit.edu
Personal Blizog: http://arcanelinux.org/blog/dlworld
The Enflame Project: http://enflame.org
Nov 15 '05 #5
On Tue, 05 Jul 2005 04:46:29 -0700, Abhi wrote:
The equation you have provided assists my understanding. Just a note
that, the word_size is not always in block of 4. It depends on what
data type we use (either int or char).

typedef struct {
int a:7;
int a1:1;
} AA;

typedef struct {
char b:7;
char b1:1;
} BB;
However this second version is not standard and therefore not portable. C
only specifies the types int, signed int and unsigned int for bit-fields.
There is an oddity for plain int for bit fields where the implementation
can make it signed or unsigned, whereas it is signed in all other contexts.
in above two structures AA has sizeof 4 bytes, while BB has 1 byte.


Your implementation is at liberty to do that but this isn't guaranteed in
general.

Lawrence
Nov 15 '05 #6
David Lago wrote:
Abhi wrote:
would define a variable of 2-bit length. My doubt is that what would
the size of this structure then? Would it be sizeof(int) or just 1
byte?

The actual size of the struct depends on your compiler, and which amount
of space it allocates for each word of data. In gcc, it allocates space
in chunks of 4 bytes (if you exceed 32 bits, it allocates a new one). So

No David, gcc doesn't do that. Try this and tell me what you get..

#include <stdio.h>

struct c {
char c;
} c;

struct s {
short s;
} s;

int main(void) {
printf("Struct c is %d byte, s is %d bytes.\n",
(int)sizeof (c), (int)sizeof (s));
return 0;
}

I get.. Struct c is 1 byte, s is 2 bytes.
actual_size = size + word_size - size % word_size

This equation does not take in account the padding fields (fields which
bit size is 0, used so as to move next field to the next word boundary).

Regards,
David.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 15 '05 #7
I quote myself on a previous post:

Extracted from "The ANSI C Programming Language, 2nd Ed." by Kernighan &
Ritchie:

[...] Fields may be declared only as ints; for portability, specify
signed or unsigned explicitly.

David.

Joe Wright wrote:
No David, gcc doesn't do that. Try this and tell me what you get..

#include <stdio.h>

struct c {
char c;
} c;

struct s {
short s;
} s;

int main(void) {
printf("Struct c is %d byte, s is %d bytes.\n",
(int)sizeof (c), (int)sizeof (s));
return 0;
}

I get.. Struct c is 1 byte, s is 2 bytes.

--
David Lago <dl***@mundo-r.com>

PGP key available at: http://pgp.mit.edu
Personal Blizog: http://arcanelinux.org/blog/dlworld
The Enflame Project: http://enflame.org
Nov 15 '05 #8
On Tue, 05 Jul 2005 17:37:11 -0400, Joe Wright wrote:
David Lago wrote:
Abhi wrote:
would define a variable of 2-bit length. My doubt is that what would
the size of this structure then? Would it be sizeof(int) or just 1
byte?

The actual size of the struct depends on your compiler, and which amount
of space it allocates for each word of data. In gcc, it allocates space
in chunks of 4 bytes (if you exceed 32 bits, it allocates a new one). So

No David, gcc doesn't do that. Try this and tell me what you get..

#include <stdio.h>

struct c {
char c;
} c;

struct s {
short s;
} s;


You are correct that this is not a general structure allocation strategy,
but the topic here but given the topic of the thread we're probably
talking about the allocation of bit-fields.

Lawrence

Nov 15 '05 #9
On Wed, 06 Jul 2005 10:10:23 +0200, David Lago
<dl***@mundo-r.com> wrote:
I quote myself on a previous post:

Extracted from "The ANSI C Programming Language, 2nd Ed." by Kernighan &
Ritchie:

[...] Fields may be declared only as ints; for portability, specify
signed or unsigned explicitly.


Correct but irrelevant. The fields must be declared as int (in C90; C99
also allows _Bool), so:

struct fred
{
int x : 2; /* OK */
char y : 1; /* error */
float z : 12; /* error */
_Bool q : 1; /* OK iff C99 */
};

but the standard (and K&R) places no constraints on what size is
actually allocated:

6.7.2.1 Structure and union specifiers

10 An implementation may allocate any addressable storage unit large
enough to hold a bit-field. If enough space remains, a bit-field
that immediately follows another bit-field in a structure shall be
packed into adjacent bits of the same unit. If insufficient space
remains, whether a bit-field that does not fit is put into the next
unit or overlaps adjacent units is implementation-defined. The
order of allocation of bit-fields within a unit (high-order to
low-order or low-order to high-order) is implementation-defined.
The alignment of the addressable storage unit is unspecified.

[INCITS/ISO/IEC 9899-1999]

For that matter a compiler might have a minimum allocation size for any
struct regardless of what fields are inside it (so a struct containing
only one byte might still take up 4 or 8 bytes, for instance). As long
as the fields within a structure as aligned as necessary (and note that
bitfields may not be aligned even to byte boundaries).

Chris C
Nov 15 '05 #10
Lawrence Kirby wrote:
On Tue, 05 Jul 2005 17:37:11 -0400, Joe Wright wrote:

David Lago wrote:
Abhi wrote:
would define a variable of 2-bit length. My doubt is that what would
the size of this structure then? Would it be sizeof(int) or just 1
byte?
The actual size of the struct depends on your compiler, and which amount
of space it allocates for each word of data. In gcc, it allocates space
in chunks of 4 bytes (if you exceed 32 bits, it allocates a new one). So


No David, gcc doesn't do that. Try this and tell me what you get..

#include <stdio.h>

struct c {
char c;
} c;

struct s {
short s;
} s;

You are correct that this is not a general structure allocation strategy,
but the topic here but given the topic of the thread we're probably
talking about the allocation of bit-fields.

Lawrence


You're right of course. Apologies to David.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 15 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Brian | last post by:
Can any one direct me to sources for best practices of field types and sizes to use for commonly used information such as address, names, city, business names .... Thanks, Brian
1
by: Catherine Jo Morgan | last post by:
I notice that different sample databases and books advise different lengths for fields such as FirstName, LastName, City, etc. Obviously it's better to err in the direction of too much space,...
13
by: Madison Kelly | last post by:
Hi again, Something I have been wondering about and haven't found an answer to yet is how the size of a datatype (I hope that is the right term) effects performance. What effect is there if I...
11
by: Tore Halset | last post by:
Hello. I am trying to port an old java application from MS SQL Server to PostgreSQL running on Mac OS X. I have access to the java source code and can make modifications. I have tried with...
60
by: deko | last post by:
As I understand it, most browser manufacturers have agreed on 16px for their default font size. So, this should be an accurate conversion for percentages: px % 16 = 100 14 = 87.5 13 =...
1
by: Edward | last post by:
I created a simple CSS layout (code and example below) for bloggin/writing but ran into five issues that I need help with: 1. How do I get rid of the right-margin red line on the last three...
1
by: PeterF | last post by:
I would like to write an SQL query that would bring back a list of all the tables, fields, data types and sizes in Access. I can do this in SQL server but cannot find the field information in the...
20
by: subramanian100in | last post by:
Suppose there are five DISTINCT types TYPE1, TYPE2, TYPE3, TYPE4, TYPE5, of data. Consider the structures: struct struct_one { TYPE1 data1; TYPE2 data2; TYPE3 data3;
3
by: Oenone | last post by:
I have a project that creates a SqlDataAdapter and uses its Fill method to fill a DataTable with data from a user-provided query. From there I can obviously access details about the rows and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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: 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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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.