Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   sizes of bit fields (http://www.velocityreviews.com/forums/t438610-sizes-of-bit-fields.html)

Abhi 07-05-2005 10:03 AM

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


David Lago 07-05-2005 10:22 AM

Re: sizes of bit fields
 
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 <dlago@mundo-r.com>

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

Abhi 07-05-2005 11:46 AM

Re: sizes of bit fields
 
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


David Lago 07-05-2005 11:55 AM

Re: sizes of bit fields
 
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 <dlago@mundo-r.com>

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

Lawrence Kirby 07-05-2005 12:34 PM

Re: sizes of bit fields
 
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 07-05-2005 06:30 PM

Re: sizes of bit fields
 
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

Joe Wright 07-05-2005 09:37 PM

Re: sizes of bit fields
 
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 ---

David Lago 07-06-2005 08:10 AM

Re: sizes of bit fields
 
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 <dlago@mundo-r.com>

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

Chris Croughton 07-06-2005 10:43 AM

Re: sizes of bit fields
 
On Wed, 06 Jul 2005 10:10:23 +0200, David Lago
<dlago@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

Lawrence Kirby 07-06-2005 12:01 PM

Re: sizes of bit fields
 
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



All times are GMT. The time now is 05:30 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.