hello i was reading today some stuff abt bitfield but here in this program I understand most of it but there unnamed fields inside the structure book says its stuff for allgiment or something like that
i dont rlly get what does it do

/* fields.c -- define and use fields */

#include <stdio.h>

/* opaque and show */

#define YES     1

#define NO      0

/* line styles     */

#define SOLID   0

#define DOTTED  1

#define DASHED  2

/* primary colors  */

#define BLUE    4

#define GREEN   2

#define RED     1

/* mixed colors    */

#define BLACK   0

#define YELLOW  (RED | GREEN)

#define MAGENTA (RED | BLUE)

#define CYAN    (GREEN | BLUE)

#define WHITE   (RED | GREEN | BLUE)

const char * colors[8] = {"black", "red", "green", "yellow",

            "blue", "magenta", "cyan", "white"};



struct box_props {

    unsigned int opaque         : 1;

    unsigned int fill_color     : 3;

    unsigned int                : 4;

    unsigned int show_border    : 1;

    unsigned int border_color   : 3;

    unsigned int border_style   : 2;

    unsigned int                : 2;

};



void show_settings(const struct box_props * pb);



int main(void)

{

    /* create and initialize box_props structure */

    struct box_props box = {YES, YELLOW , YES, GREEN, DASHED};



    printf("Original box settings:\n");

    show_settings(&box);



    box.opaque = NO;

    box.fill_color = WHITE;

    box.border_color = MAGENTA;

    box.border_style = SOLID;

    printf("\nModified box settings:\n");

    show_settings(&box);



    return 0;

}



void show_settings(const struct box_props * pb)

{

    printf("Box is %s.\n",

            pb->opaque == YES? "opaque": "transparent");

    printf("The fill color is %s.\n", colors[pb->fill_color]);

    printf("Border %s.\n",

            pb->show_border == YES? "shown" : "not shown");

    printf("The border color is %s.\n", colors[pb->border_color]);

    printf ("The border style is ");

    switch(pb->border_style)

    {

        case SOLID  : printf("solid.\n"); break;

        case DOTTED : printf("dotted.\n"); break;

        case DASHED : printf("dashed.\n"); break;

        default     : printf("unknown type.\n"); break;

    }

}

I understand all code here but what i dont understand what those unnamed fields are used for ....

Recommended Answers

All 11 Replies

The unnamed fields may just be padding so that the structure's data is laid out correctly...Gerard4143

The unnamed fields may just be padding so that the structure's data is laid out correctly...Gerard4143

Yes, structure goes by units of 4.

Its not always. The deciding factor is machine type(32bit or 64bit).
So for 32 bit machine boundary condition is double word and for 64bit machine it's quad word. Address calculation by processor is easy if words are stored at boundary and hence minimum number of cycles required to fetch the word.
Always use

sizeof

operator to make your code portable.

The unnamed fields may just be padding so that the structure's data is laid out correctly...Gerard4143

it optimize the structure you mean ? or what do you mean by padding ?

let a struct is declared as

struct abc{
int a;
char b;
int c;
};

let assume memory for this structure starts at 0x00000000,
then memory layout will be as follows:-
0x00000000 - 0x00000003 : int a
0x00000004 - 0x00000005: char b
0x00000006 - 0x00000007: 0 (this is padding)
0x00000008 - 0x0000000b: int c

for 64bit machine
0x00000000 - 0x00000007 : int a
0x00000008 - 0x00000009: char b
0x0000000a - 0x000000010: 0 (this is padding)
0x00000011 - 0x00000017: int c

in above case if all the bit fields sums less than or equal to 32 bit (size of int, assuming unix, machines, in turbo C compiler sizeof int is 2 bytes) 4 bytes will be allocated for that.

let a struct is declared as


let assume memory for this structure starts at 0x00000000,
then memory layout will be as follows:-
0x00000000 - 0x00000003 : int a
0x00000004 - 0x00000005: char b
0x00000006 - 0x00000007: 0 (this is padding)
0x00000008 - 0x0000000b: int c

for 64bit machine
0x00000000 - 0x00000007 : int a
0x00000008 - 0x00000009: char b
0x0000000a - 0x000000010: 0 (this is padding)
0x00000011 - 0x00000017: int c

in above case if all the bit fields sums less than or equal to 32 bit (size of int, assuming unix, machines, in turbo C compiler sizeof int is 2 bytes) 4 bytes will be allocated for that.

The above lay out only works if the structure is packed. Just try and take sizeof the above structures and you'll see that memory alignment will increase the size allotted for the char variable to word size...

That's what i've said. Here sizeof(struct abc) will give 12 bytes. This is what i've shown in memory layout.

oh but why does every bitfield structure has int size can it have a smaller size?

oh but why does every bitfield structure has int size can it have a smaller size?

Yes you can pack the structure, then the size of the structure is the sum of its elements. The reason the bit fields are aligned(or word size) is the CPU can read aligned data more efficiently, so unless you request a packed structure the compiler will use word size where it can....Gerard4143

k thanks a lot guys for the information

if you declare only char types in struct you will find that sizeof will give the exact size what you have declared. But once you start including int, floats and then also char, compiler will start considering boundary alignment.
just play with struct and bitfields.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.