# sizes of bit fields

Discussion in 'C Programming' started by Abhi, Jul 5, 2005.

1. ### AbhiGuest

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

Abhi, Jul 5, 2005

2. ### David LagoGuest

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 <>

PGP key available at: http://pgp.mit.edu
Personal Blizog: http://arcanelinux.org/blog/dlworld
The Enflame Project: http://enflame.org

David Lago, Jul 5, 2005

3. ### AbhiGuest

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

Abhi, Jul 5, 2005
4. ### David LagoGuest

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 <>

PGP key available at: http://pgp.mit.edu
Personal Blizog: http://arcanelinux.org/blog/dlworld
The Enflame Project: http://enflame.org

David Lago, Jul 5, 2005
5. ### Lawrence KirbyGuest

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

Lawrence Kirby, Jul 5, 2005
6. ### Lawrence KirbyGuest

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

Lawrence Kirby, Jul 5, 2005
7. ### Joe WrightGuest

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 ---

Joe Wright, Jul 5, 2005
8. ### David LagoGuest

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 <>

PGP key available at: http://pgp.mit.edu
Personal Blizog: http://arcanelinux.org/blog/dlworld
The Enflame Project: http://enflame.org

David Lago, Jul 6, 2005
9. ### Chris CroughtonGuest

On Wed, 06 Jul 2005 10:10:23 +0200, David Lago
<> 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

Chris Croughton, Jul 6, 2005
10. ### Lawrence KirbyGuest

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

Lawrence Kirby, Jul 6, 2005
11. ### Joe WrightGuest

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 ---

Joe Wright, Jul 7, 2005