sizes of bit fields

A

Abhi

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
 
D

David Lago

Abhi said:
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 <[email protected]>

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

Abhi

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
 
D

David Lago

Abhi said:
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 <[email protected]>

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

Lawrence Kirby

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
 
L

Lawrence Kirby

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
 
J

Joe Wright

David said:
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.
 
D

David Lago

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 said:
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 <[email protected]>

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

Chris Croughton

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
 
L

Lawrence Kirby

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
 
J

Joe Wright

Lawrence said:
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.
 

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

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top