int main (int argc, char * argv[])
{[INDENT] struct
{[INDENT] short Signture;
long FileSize;
short Reserved1;
short Reserved2;
long ImgDataStartOff;
long InfoHeaderSize;
long Width;
long Height;
short Planes;
short NumbOfPix;
long Compression;
long SizeOfImgData;
long HRes;
long VRes;
long NumOfCol;
long NumOfImpCol;[/INDENT] } Header;
cout<<"Size of Header = "<nul");
return EXIT_SUCCESS;[/INDENT]}
I'm using Dev-CPP compiler.
Output is 56, but it must be 54.
9.10: Why does sizeof report a larger size than I expect for a structure type, as if there was padding at the end?
Structures may have this padding (as well as internal padding; see also question 9.5 ), so that alignment properties will be preserved when an array of contiguous structures is allocated.
So, is that the fault of sizeof() or the structure really holds 56 bytes?
I am gonna use this structure to gether the header of a bitmap file. My question is, am I gonna get 54 bytes or 56 bytes by a file stream function such as .read() ?
So, is that the fault of sizeof() or the structure really holds 56 bytes?
I am gonna use this structure to gether the header of a bitmap file. My question is, am I gonna get 54 bytes or 56 bytes by a file stream function such as .read() ?
Whatsizeof tells you is the truth.
Look into your compiler's documentation for a way to pack structures.
Are there any gaps between variables in a structure? If so, how can we avoid them?
I take it you didn't follow the link that already answered this question, nor did you take my advice about checking your compiler's documentation. Why ask for help and ignore the replies?
I made some test on my code. Here is a sample :
#include
#include
int main (int argc, char * argv[])
{
struct
{
short Signture;
long FileSize;
short Reserved1;
short Reserved2;
long ImgDataStartOff;
long InfoHeaderSize;
long Width;
long Height;
short Planes;
short NumOfPix;
long Compression;
long SizeOfImgData;
long HRes;
long VRes;
long NumOfCol;
long NumOfImpCol;
} Header;
memset(&Header, 196, 75); //Fill the structure with '-' characters
cout<<"Size of Header = "<nul");
return EXIT_SUCCESS;
}
The output is :Size of Header = 56
0 A
1 B
2 ─
3 ─
4 C
5 D
6 E
7 F
8 G
9 H
10 I
11 J
12 K
13 L
14 M
15 N
16 O
17 P
18 Q
19 R
20 S
21 T
22 U
23 V
24 W
25 X
26 Y
27 Z
28 [
29 \
30 ]
31 ^
32 _
33 `
34 a
35 b
36 c
37 d
38 e
39 f
40 g
41 h
42 i
43 j
44 k
45 l
46 m
47 n
48 o
49 p
50 q
51 r
52 s
53 t
54 u
55 v
56 ─
57 ─
58 ─
59 ─
As it is seen, the gap is between the variables "Signture" and "FileSize". There is a 2 byte gap.
But I managed to solve my problem. I used pre-defined windows structures which I found on MSDN.
As mentioned in the links, there are ways to "pack" structures. There is no standard way, however, so each system may do it differently. One way might be #pragma pack . Then again it might not. Only your compiler's documentation will tell you (and you might want to mention which compiler you are using for the rest of us that can't see your computer).
that is correct frnd. u just try one more long double or double variable it will show even higher value. its bcoz struct wil allocate memory arrays in terms of the longest variable(member) (may be a double,long,etc.,.)of the struct.In your problem it will allocate arrays of contigous memory in terms of size of long.
if u hv any doubt just add one more short varible it will remain same and u add even more one the size wil increas by 4. just try........ these all r bcoz of ur os.
ans:
11*4=44
10 more required for int. but in terms of long type it will allocate memory>=10 that is 3*4=12 which is >=10.
tot=44+12=56