Showing posts with label Structure and unions. Show all posts
Showing posts with label Structure and unions. Show all posts

Sunday, November 10, 2013

C programs Structure union bit-fields!!

In C Programming Language, a structure is a user defined data type, which is a collection of different data types. Strutures, unions and bitfields are conceptually same, the difference in memory alignment. So defferentiate them, we need to get the size of the structure or union. Below are the sample c programs for each of them. Structures also do padding, Click here to get how padding works.

For below examples, size of int is 4 bytes(some systems allocates 2 or 8. ) and character takes 1 byte.

Structures in C: Below structure is collection of one char and one int data types. So total size should be 5 bytes(1+4). Because of memory alignment, it will allocate 4 bytes for char also, but 3 of 4 bytes in char variable or unused. So we will get the size of the structure as 8.

#include<stdio.h>
struct Sample{
 char c;
 int n;
};
int main()
{
 struct Sample s;
 printf("size is %u\n",sizeof(s));
}
Output:
$ ./a.out
size is 8

Unions in C: Unions are also same as structures. But the difference in memory alignment. The size of the union is the maximum size of the data type in the union. For example in the below example, union contains one char variable and one int variable, so char takes 1 byte and int takes 4 bytes, max of (1,4) is 4 , so size of the union is 4 bytes.

#include<stdio.h>
union Sample{
 char c;
 int n;
};
int main()
{
 union Sample s;
 printf("size is %u\n",sizeof(s));
}
Output:
$ ./a.out
size is 4

Bit fields in C: In the structure example we have seen, 3 bytes are unused because of memory alignment and padding. So to avoid these unused bytes, bit fields are introduced. We can specify, required bits using bitfields as shown below. We need to use colon(:) after the variable name, and specify the number of bits.

In the below example, we specified 2 bits for char and 2 bits for int, so total 4 bits (and not bytes), total size should be 4 bits, but because of memory alignment and padding, we will get 4 bytes as shown below. We will get 4 bytes until all the bits in the 4 bytes (32 bits) are used.

#include<stdio.h>
struct Sample{
 char c:2;
 int n:2;
};
int main()
{
 struct Sample s;
 printf("size is %u\n",sizeof(s));
}

Output:
$ ./a.out
size is 4

How structure padding works?

Structures in C programming language are collection of different data types. Click here for more details about structures. C compiler will structure padding for the structures in the memory alignment..

Why structure padding: Structure padding will be used for fast execution. Generally memory allocation will be done basing on the size of the int for the fast execution. So to achieve this rule, if the size of the memory space is less than the size of the int (for example char it takes 1 byte), padding will be done by adding required bytes.

Lets look at the below example: In the example, structure contains char (1byte) and int(4 bytes), so total size should be 5 bytes. But because of memory alignment for fast execution, compiler will be added extra 3 bytes as a padding, and the total size of the structure will be 8 bytes.

Sample C program for Structures:
#include<stdio.h>
struct Sample{
 char c;
 int n;
};
int main()
{
 struct Sample s;
 printf("size is %u\n",sizeof(s));
}

Output:
$ ./a.out
size is 8

Look at another example: In the below example we added extra char variable c2, still the size of the structure is same. Because char takes one byte and two chars are in sequential, so size is (1+1+2padding+4), so total size is 8 bytes.

#include<stdio.h>
struct Sample{
 char c1;
 char c2;
 int n;
};
int main()
{
 struct Sample s;
 printf("size is %u\n",sizeof(s));
}

Output:
$ ./a.out
size is 8

Look at another example: In the below example, we added the extra char c2 after the int variable and the size of the structure is now 12 bytes. This is also due to padding. char+int+char which is equal to 1+3(padding)+4+1+3(padding).

In the above example, two chars and int takes 8 bytes, but here two chars and int takes 12 bytes. This is because of order of the variables used in the structure. For structure padding, order of the variables are also very important.

So in the below example, first char 1byte and no chars are there after it, so 3bytes for padding, for int b bytes and for another char c2 one byte and 3 padding bytes. If another char is there after c2, only 2 bytes padding will be required.

#include<stdio.h>
struct Sample{
 char c1;
 int n;
 char c2;
}; int main() { struct Sample s; printf("size is %u\n",sizeof(s)); }

Output:
$ ./a.out
size is 12

Subscribe to: Posts (Atom)

Popular Posts

AltStyle によって変換されたページ (->オリジナル) /