C Programming :: Floating Point Problems - Discussion
5 / 6
#include <stdio.h>
# pragma pack(2)
struct SIZE {
int i;
char ch ;
double db ;
} ;
main () {
printf ( "%d\n",sizeof(struct SIZE) );
}
#include <stdio.h>
# pragma pack(2)
struct SIZE {
int i;
char ch ;
double db ;
} ;
main () {
printf ( "%d\n",sizeof(struct SIZE) );
}
A12
B14
C16
D8
Show Explanation
Pragma pack instructs the compiler to pack structure members with particular alignment. Most compilers, when you declare a struct, will insert padding between members to ensure that they are aligned to appropriate addresses in memory (usually a multiple of the type's size). This avoids the performance penalty (or outright error) on some architectures associated with accessing variables that are not aligned properly. For example, given 4-byte integers and the following struct:
struct SIZE {
int i;
char ch ;
double db ;
} ;
The compiler could choose to lay the struct out in memory like this:
1 byte | 2nd byte | 3rd byte | 4th byte | |
int i | i(1) | i(2) | i(3) | i(4) |
char ch | ch(1) | padding | padding | padding |
double db | db(1) db(5) | db(2) db(6) | db(3) db(7) | db(4) db(8) |
the size of this structure will be 16.
with pragma pack (1)
1 byte
int i(1)
int i(2)
int i(3)
int i(4)
char ch(1)
double db(1)
.
.
double db(8)
and the size of strut will be 13.
with pragma pack (2)
1st byte 2nd byte
int i(1) int i(2)
int i(3) int i(4)
char ch(1) double db(1)
. .
double db(6) double db(7)
double db(8) padding
the size of struct will be 14 (Answer). if pragma pack (4) is given then in 32 bit size of sruct will be 16.
pragma pack tells the compiler the boundary to align objects in a structure to in order to improve access times.
Asked In ::
Pragma pack instructs the compiler to pack structure members with particular alignment. Most compilers, when you declare a struct, will insert padding between members to ensure that they are aligned to appropriate addresses in memory (usually a multiple of the type's size). This avoids the performance penalty (or outright error) on some architectures associated with accessing variables that are not aligned properly. For example, given 4-byte integers and the following struct:
struct SIZE {
int i;
char ch ;
double db ;
} ;
The compiler could choose to lay the struct out in memory like this:
1 byte | 2nd byte | 3rd byte | 4th byte | |
int i | i(1) | i(2) | i(3) | i(4) |
char ch | ch(1) | padding | padding | padding |
double db | db(1) db(5) | db(2) db(6) | db(3) db(7) | db(4) db(8) |
the size of this structure will be 16.
with pragma pack (1)
1 byte
int i(1)
int i(2)
int i(3)
int i(4)
char ch(1)
double db(1)
.
.
double db(8)
and the size of strut will be 13.
with pragma pack (2)
1st byte 2nd byte
int i(1) int i(2)
int i(3) int i(4)
char ch(1) double db(1)
. .
double db(6) double db(7)
double db(8) padding
the size of struct will be 14 (Answer). if pragma pack (4) is given then in 32 bit size of sruct will be 16.
pragma pack tells the compiler the boundary to align objects in a structure to in order to improve access times.
Read Full Answer
Report Error
Please Login First Click Here