Bit-fields confusion? - c

I am working with bit-fields in C and do not understand what is going on with them. I created this code but I do not understand why different things are coming up as usual.
struct tB
{
unsigned b1:3;
signed b2:6;
unsigned b3:11;
signed b4:1;
} b;
int main(void)
{
struct tB *p;
printf("%d\n", sizeof(*p));
}
Why when I print out *p do I get 4 as *p?
Let us say I was trying to get sizeof(b), how would I come up with that?

sizeof(b) will give you the size in bytes of a variable of type struct tB,which in this case will be 4 (Due to padding it won't be 3 as it is expected to be)
sizeof(*p) will again give you the size in bytes of a variable of type struct tB .You should initialize p with the address of a variable of struct tB type.Eg:
struct tB *p=&b;
But you should know that in this case if you use sizeof(p) then it would give the size of the pointer p, not the variable pointed by p. Try this variation of your program :
#include<stdio.h>
struct tB
{
unsigned b1:3;
signed b2:6;
unsigned b3:11;
signed b4:1;
unsigned b5:13;
} b;
int main(void)
{
struct tB *p;
printf("%d\n%d",sizeof(*p),sizeof(p));
}
Here is another variation that rounds the size of struct tB to 24 bits(3 bytes) as you expect,by dealing with the padding using the #pragma pack() directive,which is compiler dependent (I am using CodeBlocks on Windows).
#include<stdio.h>
#pragma pack(1)
struct tB
{
unsigned b1:3;
signed b2:6;
unsigned b3:11;
signed b4:1;
} b;
int main(void)
{
struct tB *p;
printf("%d\n%d",sizeof(*p),sizeof(p));
}

You have 21 bits, round up to nearest int and you got 32 (i.e. 4 bytes).

It's all about processor word. The processor accessing the memory can't access it, let's say, 1 or 2 bytes. It fetching it by word. Generally, compiler makes proper aligning of structures to conform word alignment. Usually, this alignment is equal to processor architecture though the size of processor register. So, in your case you have 21-bit structure which aligned to one word. If you will adjust your structure to be, say, 33-bits long you will have 2-word alignment and in your case program will print 8.
Here the article on Wikipedia related to this Data structure alignment.

Related

Why is this structures code in C outputs differently? [duplicate]

I've read this about structure padding in C:
http://bytes.com/topic/c/answers/543879-what-structure-padding
and wrote this code after the article, what should print out size of 'struct pad' like 16 byte and the size of 'struct pad2' should be 12. -as I think.
I compiled this code with gcc, with different levels of optimization, even the sizeof() operator gives me both of them 16 byte.
Why is it?
This information is necessary for me because of PS3 machines, where the byte boundaries and exploitation of the full dma transfer is important:
#include <stdio.h>
#include <stdlib.h>
struct pad
{
char c1; // 1 byte
short s1; // 2 byte
short s2; // 2 byte
char c2; // 1 byte
long l1; // 4 byte
char c3; // 1 byte
};
struct pad2
{
long l1;
short s1;
short s2;
char c1;
char c2;
char c3;
};
int main(void)
{
struct pad P1;
printf("%d\n", sizeof(P1));
struct pad P2;
printf("%d\n", sizeof(P2));
return EXIT_SUCCESS;
}
There are two tricks that can be used to owercome this problem
Using directive #pragma pack(1) and then #pragma pack(pop)
example:
#pragma pack(1)
struct tight{
short element_1;
int *element_2;
};
#pragma pack(pop)
To check if the sizes of two structs are same during compilation use this trick
char voidstr[(sizeof(struct1)==sizeof(struct2)) - 1]; //it will return error at compile time if this fail
Your structures each include a long, which your platform apparently requires to be on a four-byte boundary. The structure must be at least as aligned as its most aligned member, so it has to be 4-byte aligned, and a structure's size has to be a multiple of its alignment in case it goes into an array.
Extra padding is required to make the long aligned, and so the smallest multiple of 4 is 16.
Two pieces of advice:
You can compute the offset of a field l1 by
printf("Offset of field %s is %d\n", "l1", offsetof(struct pad, l1);
To get the offsetof macro you will need to #include <stddef.h> (thanks caf!).
If you want to pack data as densely as possible, use unsigned char[4] instead of long and unsigned char[2] instead of short, and do the arithmetic to convert.
EDIT:: The sizeof(struct pad2) is 12. Your code has a bug; structure P2 is declared of type struct pad. Try this:
#define xx(T) printf("sizeof(" #T ") == %d\n", sizeof(T))
xx(struct pad);
xx(struct pad2);
P.S. I should definitely stop trying to answer SO questions after midnight.
On PS3, don't guess. Use __attribute__((aligned (16))), or similar. Not only does it guarantee that the start of the structure will be aligned on a proper boundary (if global or static), it also pads the structure to a multiple of your specified alignment.
Your code isn't showing what you think it is, because both P1 and P2 are defined as instances of struct pad. struct pad2 isn't ever used.
If I change the definition of P2 so that it is struct pad2, gcc does indeed decide to make it size 12.
struct pad P1;
printf("%d\n", sizeof(P1));
struct pad P2;
printf("%d\n", sizeof(P2));
P1 and P2 have the same type "struct pad" maybe you want to use "struct pad2" for P2.
All CPU expect that the built in data types like (int, float,char,double) are stored in the memory at their natural boundary, at address of their length.So structure padding is done to faster access of data from memory.
For example,
If int is declared, it should occurs in the memory at an address multiple of 4, as
size of the int is 4 byte.
Similarly for double, it resides in memory at multiple of 8.
If memory is properly aligned, CPU can run faster and work efficiently.
For the following examples, let us assume:
Sizeof(int)=4 byte
Sizeof(float)=4 byte
Sizeof(char)=1 byte
Find details on BoundsCheck

union and structure memory sharing

I was trying to find the answer of a question which involves two structures inside a union.
I have typed the question in online debugger the answer which I am finding and the real answer is not giving hands to each other. Can somebody help me out with the answer with explanation.
#include <stdio.h>
int main()
{
union{
struct{
char c[2];
char ch[2];
}s;
struct {
short int i;
short int j;
}st;
}u={12,1,15,1};
printf("%d%d",u.st.i,u.st.j);
}
answer is 268 and 271
Decimal numbers are not very good for this kind of tests. You should also enable all warnings as well. If you do so you will get
<source>:25:8: warning: missing braces around initializer [-Wmissing-braces]
So let's rewrite your program a little bit:
int main()
{
union
{
struct
{
char c[2];
char ch[2];
}s;
struct
{
short int i;
short int j;
}st;
}u={.s = {{0x12,0x01},{0x15,0x01}}}; //better to show compiler what you want to initialize
printf("%04hx %04hx",u.st.i,u.st.j);
}
And the result is:
0112 0115
clearly showing the numbers we have used in the initialization. The numbers show us that this system uses little endian system where the least significant byte is placed first.
You should be avare that compiler is free to add any padding needed. To avoid it you have to use compiler extensions - structure packing.
Start by making more sense of this by swapping to hexadecimal: {0xC,0x1,0xF,0x1}. This allows us to view the bytes as they are stored in memory, without worrying about converting to/from decimal base.
When initializing a union, the first member gets initialized, in this case the struct s.
It would be cleaner to write { {{0xC,0x1}, {0xF,0x1}} } but unfortunately C allows omitting the curly braces of inner structs/arrays, allowing us to put sloppily written crap in our initialization lists just because we can.
The memory order of a char byte array is always top to bottom on any system: first byte on least significant address. That is: 0xC 0x1 0xF 0x1.
Strictly speaking, the compiler is free to add struct padding bytes anywhere between the struct/union members, to compensate for misalignment. Doesn't happen in this specific case, but it would happen if we for example used char c[3].
When you "type pun" this memory area using shorts, they get read according to endianess, since short is larger than 1 byte and an integer type.
On a little endian CPU you would therefore get each short to assume that 0xC is the least significant byte and so the first short ends up as 010C hex.
If we translate your code to hex, it should be clearer:
#include <stdio.h>
int main()
{
union{
struct{
char c[2];
char ch[2];
}s;
struct {
short int i;
short int j;
}st;
}u={ {{0xC,0x1}, {0xF,0x1}} };
printf("%.4X\n%.4X",u.st.i,u.st.j);
}
Output on little endian CPU:
010C
010F
10Ch = 268d and so on.

Default structure alignment for 32 bit processor word

I'm working with an struct compiled for a 32bit ARM processor.
typedef struct structure {
short a;
char b;
double c;
int d;
char e;
}structure_t;
If I use nothing, __attribute__ ((aligned (8))) or__attribute__ ((aligned (4))) I get the same results in terms of structure size and elements offset. Total size is 24. So I think it is always aligning to 8 (offsets are for both a=0 , b=2, c=8, d=16, e=20).
Why is 8 the default alignment chosen by the compiler? Should not it be 4 because is a 32 word processor?
Thanks in advance mates.
The aligned attribute only specifies a minimum alignment, not an exact one. From gcc documentation:
The aligned attribute can only increase the alignment; but you can decrease it by specifying packed as well.
And the natural alignment of double is 8 on your platform, so that is what is used.
So to get what you want you need to combine the aligned and packed attributes. With the following code, c has offset 4 (tested using offsetof).
typedef struct structure {
short a;
char b;
__attribute__((aligned(4))) __attribute__((packed)) double c;
int d;
char e;
} structure_t;

How do the bit-fields work exactly in these C programs?

I am a bit confused with the output of the following code although I know what the declaration of such struct means in C.
#include<stdio.h>
struct struc
{
int a:1;
int b:3;
int c:6;
int d:3;
}s1;
struct stru
{
char a:3;
}s2;
int main()
{
printf("%lu %lu",sizeof(s1),sizeof(s2));
getchar();
return 0;
}
I am trying to learn the actual working of this type of structure declaration.
Can anyone explain how the code is giving output as "4 1", as I want to get the grasp over the concept properly.
And also the following code:
#include <stdio.h>
struct marks{
int p:3;
int c:3;
int m:2;
};
int main(void) {
struct marks s={2,-6,5};
printf("%d %d %d",s.p,s.c,s.m);
return 0;
}
What should be the output from this program?
The code you show is using bit fields. In a nutshell, :n after a struct member restricts this member to use only n bits, and such members are "packed" together when adjacent members have the same type.
A long time ago, this was sometimes useful to save memory -- nowadays, you might need it in very low-level hardware interfacing code, but that's probably about it.
What happens here:
struct struc
{
int a:1;
int b:3;
int c:6;
int d:3;
}s1;
This struct only has 1 + 3 + 6 + 3 = 13 bits worth of information. Assuming your implementation uses 32 bit int and a char has 8 bits, the sizeof int is 4 (32 / 8), and this is still enough to store all the bits of your members here. So the size of the whole struct is still only 4.
Note that all of this is implementation defined -- it depends on the sizes of char and int and the compiler is still free to add padding as needed. So using bitfields requires you to know what exactly your implementation is doing, at least if you need to rely on sizes and the exact layout of the bits.
In the example we can see that the 1st struct uses int and the 2nd struct uses a char.
The char gives a size result of 1. Which I think is as expected.
If we look at the sizes of variables :
cpp reference - types
Then we can see that an int can take up 16 or 32 bits.
As size of your struct is 4 we can determine that your compiler uses 32 bits for this storage.

Structure padding in C

I've read this about structure padding in C:
http://bytes.com/topic/c/answers/543879-what-structure-padding
and wrote this code after the article, what should print out size of 'struct pad' like 16 byte and the size of 'struct pad2' should be 12. -as I think.
I compiled this code with gcc, with different levels of optimization, even the sizeof() operator gives me both of them 16 byte.
Why is it?
This information is necessary for me because of PS3 machines, where the byte boundaries and exploitation of the full dma transfer is important:
#include <stdio.h>
#include <stdlib.h>
struct pad
{
char c1; // 1 byte
short s1; // 2 byte
short s2; // 2 byte
char c2; // 1 byte
long l1; // 4 byte
char c3; // 1 byte
};
struct pad2
{
long l1;
short s1;
short s2;
char c1;
char c2;
char c3;
};
int main(void)
{
struct pad P1;
printf("%d\n", sizeof(P1));
struct pad P2;
printf("%d\n", sizeof(P2));
return EXIT_SUCCESS;
}
There are two tricks that can be used to owercome this problem
Using directive #pragma pack(1) and then #pragma pack(pop)
example:
#pragma pack(1)
struct tight{
short element_1;
int *element_2;
};
#pragma pack(pop)
To check if the sizes of two structs are same during compilation use this trick
char voidstr[(sizeof(struct1)==sizeof(struct2)) - 1]; //it will return error at compile time if this fail
Your structures each include a long, which your platform apparently requires to be on a four-byte boundary. The structure must be at least as aligned as its most aligned member, so it has to be 4-byte aligned, and a structure's size has to be a multiple of its alignment in case it goes into an array.
Extra padding is required to make the long aligned, and so the smallest multiple of 4 is 16.
Two pieces of advice:
You can compute the offset of a field l1 by
printf("Offset of field %s is %d\n", "l1", offsetof(struct pad, l1);
To get the offsetof macro you will need to #include <stddef.h> (thanks caf!).
If you want to pack data as densely as possible, use unsigned char[4] instead of long and unsigned char[2] instead of short, and do the arithmetic to convert.
EDIT:: The sizeof(struct pad2) is 12. Your code has a bug; structure P2 is declared of type struct pad. Try this:
#define xx(T) printf("sizeof(" #T ") == %d\n", sizeof(T))
xx(struct pad);
xx(struct pad2);
P.S. I should definitely stop trying to answer SO questions after midnight.
On PS3, don't guess. Use __attribute__((aligned (16))), or similar. Not only does it guarantee that the start of the structure will be aligned on a proper boundary (if global or static), it also pads the structure to a multiple of your specified alignment.
Your code isn't showing what you think it is, because both P1 and P2 are defined as instances of struct pad. struct pad2 isn't ever used.
If I change the definition of P2 so that it is struct pad2, gcc does indeed decide to make it size 12.
struct pad P1;
printf("%d\n", sizeof(P1));
struct pad P2;
printf("%d\n", sizeof(P2));
P1 and P2 have the same type "struct pad" maybe you want to use "struct pad2" for P2.
All CPU expect that the built in data types like (int, float,char,double) are stored in the memory at their natural boundary, at address of their length.So structure padding is done to faster access of data from memory.
For example,
If int is declared, it should occurs in the memory at an address multiple of 4, as
size of the int is 4 byte.
Similarly for double, it resides in memory at multiple of 8.
If memory is properly aligned, CPU can run faster and work efficiently.
For the following examples, let us assume:
Sizeof(int)=4 byte
Sizeof(float)=4 byte
Sizeof(char)=1 byte
Find details on BoundsCheck

Resources