while putting prgma(2), pragma(4) i getting same result. any body can give explanation?
Code:#include<stdio.h>
#pragma pack(2)
struct hai
{
char ch1;
char ch2;
char ch3;
char ch4;
char ch5;
}s;
int main()
{
printf("%d \n",sizeof(s));
return 0;
}
Printable View
while putting prgma(2), pragma(4) i getting same result. any body can give explanation?
Code:#include<stdio.h>
#pragma pack(2)
struct hai
{
char ch1;
char ch2;
char ch3;
char ch4;
char ch5;
}s;
int main()
{
printf("%d \n",sizeof(s));
return 0;
}
The #pragma pack directive modifies the current alignment rule for members of structures following the directive.
see the below link and u can google it man
http://publib.boulder.ibm.com/infoce...f/rnpgpack.htm
i dont think so on size #pragma will effect any thing
hi rocky,
can u check these programs
Code:#include<stdio.h>
#pragma pack(2)
struct s
{
char ch1;
char ch2;
char ch3;
char ch4;
char ch5;
char ch6;
char ch7;
};
int main()
{
struct s s1;
printf("%d\n",sizeof(s1));
return 0;
}
putput :7
output :8;Code:#include<stdio.h>
#pragma pack(2)
struct s
{
char ch1;
char ch2;
int i;
char ch;
};
int main()
{
struct s s1;
printf("%d\n",sizeof(s1));
return 0;
}
why first program output 7, second program 8?
Hi rochy,
can u check the below programs outputs
Code:#include<stdio.h>
#pragma pack(2)
struct s
{
char ch1;
char ch2;
char ch3;
char ch4;
char ch5;
char ch6;
char ch7;
};
int main()
{
struct s s1;
printf("%d\n",sizeof(s1));
return 0;
}
output :7
output 8:Code:#include<stdio.h>
#pragma pack(2)
struct s
{
char ch1;
char ch2;
int i;
char ch7;
};
int main()
{
struct s s1;
printf("%d\n",sizeof(s1));
return 0;
}
please can u let me know ur suggestins , i little bit confusion how these bit will allign in memory
hmm ..interesting it seems to allocate one extra space..when we store integer and char i tried it on double and integer..its not allocating extra space and the other data typesQuote:
Code:#include<stdio.h>
#pragma pack(2)
struct s
{
char ch1;
char ch2;
char ch3;
char ch4;
char ch5;
char ch6;
char ch7;
};
int main()
{
struct s s1;
printf("%d\n",sizeof(s1));
return 0;
}
output :7
output 8:Code:#include<stdio.h>
#pragma pack(2)
struct s
{
char ch1;
char ch2;
int i;
char ch7;
};
int main()
{
struct s s1;
printf("%d\n",sizeof(s1));
return 0;
}
please can u let me know ur suggestins , i little bit confusion how these bit will allign in memory
hey Nk Rao
check this out may be u can get your answer, and u can visit this url dude
sizeof - Wikipedia, the free encyclopedia
Quote:
Structure padding
Main article: data structure alignment
To calculate the sizes of user-defined types, the compiler takes into account any alignment space needed for complex user-defined data structures. This is why the size of a structure in C can be greater than the sum of the sizes of its members. For example, on many systems, the following code will print 8:
printf("%u", (unsigned) sizeof (struct student));Code:struct student{
char grade; /* char is 1 byte long */
int age; /* int is 4 bytes long */
};
The reason for this is that most compilers, by default, align complex data-structures to a word alignment boundary. In addition, the individual members are also aligned to their respective alignment boundaries. By this logic, the structure student gets aligned on a word boundary and the variable age within the structure is aligned with the next word address. This is accomplished by way of the compiler inserting "padding" space between two members or to the end of the structure to satisfy alignment requirements. This padding is inserted to align age with a word boundary. (Most processors can fetch an aligned word faster than they can fetch a word value that straddles multiple words in memory, and some don't support the operation at all[1]).
I think your all doubts can be cleared up now :)