Friday, February 8, 2008

Why we cannot use relation and logical operators in structure variable?

What is slack byte ?
Ans:

To store any type of data in structure there is minimum fixed byte which must be reserved by memory. This minimum byte is known as word boundary. Word boundary depends upon machine. TURBO C is based on 8086 microprocessor which has two byte word boundary. So any data type reserves at least two byte space.
Suppose a structure word1:
struct word1
{
char a;
int b;
char c;
};


First of all char a will reserve two byte and store the data in only first byte (size of char is one byte).Now int b(size of int two byte) will search two byte but there only byte is available so it will again reserve next two byte of memory space. That one byte will useless, such useless byte is know as slack byte and structure is called unbalance structure.
How we can make structure word1 as a balance structure?
Ans:

strcut word2
{
char a;
char b;
int c;

};

First of all char a will reserve two byte and store the data in only first byte .Now char b will search one byte and one byte is available so it will store the data in that byte. Now int c will reserve two byte and store the data in both two bytes. Now there is not any slack byte and we have saved wastage of memory and structure word2 is balance.

(Before executing this program first go to option menu then compiler then code generation then select word alignment then press OK)
Program:
void main()
{
struct word1
{
char a;
int b;
char c;
} ;
struct word2
{
char a;
char b;
int c;
};
clrscr();
printf("%d\t%d",sizeof(struct word1),sizeof(struct word2));

getch();
}
Output:
6 4

 
 
 
 
 

0 comment here::