sizeof() for a Structure

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2005
Posts: 71
Reputation: AhmedHan is an unknown quantity at this point 
Solved Threads: 1
AhmedHan's Avatar
AhmedHan AhmedHan is offline Offline
Junior Poster in Training

sizeof() for a Structure

 
0
  #1
Jul 28th, 2005
Here is my code :
#include <iostream.h>

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 NumbOfPix;
long Compression;
long SizeOfImgData;
long HRes;
long VRes;
long NumOfCol;
long NumOfImpCol;
} Header;

cout<<"Size of Header = "<<sizeof(Header)<<endl;

system("PAUSE>nul");
return EXIT_SUCCESS;
}
I'm using Dev-CPP compiler.
Output is 56, but it must be 54.

Any explanation and solutions?
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 466
Reputation: winbatch is on a distinguished road 
Solved Threads: 18
winbatch's Avatar
winbatch winbatch is offline Offline
Posting Pro in Training

Re: sizeof() for a Structure

 
0
  #2
Jul 28th, 2005
I found this:


http://www.eskimo.com/~scs/C-faq/q2.13.html

and this:

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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 71
Reputation: AhmedHan is an unknown quantity at this point 
Solved Threads: 1
AhmedHan's Avatar
AhmedHan AhmedHan is offline Offline
Junior Poster in Training

Re: sizeof() for a Structure

 
0
  #3
Jul 28th, 2005
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() ?
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,362
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 241
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: sizeof() for a Structure

 
0
  #4
Jul 28th, 2005
Originally Posted by AhmedHan
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() ?
What sizeof tells you is the truth.

Look into your compiler's documentation for a way to pack structures.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 71
Reputation: AhmedHan is an unknown quantity at this point 
Solved Threads: 1
AhmedHan's Avatar
AhmedHan AhmedHan is offline Offline
Junior Poster in Training

Re: sizeof() for a Structure

 
0
  #5
Jul 28th, 2005
Originally Posted by Dave Sinkula
Look into your compiler's documentation for a way to pack structures.
What do you mean by packing structures?

Are there any gaps between variables in a structure? If so, how can we avoid them?
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,362
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 241
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: sizeof() for a Structure

 
0
  #6
Jul 28th, 2005
Originally Posted by AhmedHan
What do you mean by packing 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?
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 71
Reputation: AhmedHan is an unknown quantity at this point 
Solved Threads: 1
AhmedHan's Avatar
AhmedHan AhmedHan is offline Offline
Junior Poster in Training

Re: sizeof() for a Structure

 
0
  #7
Jul 29th, 2005
Thanx Dave, you are very kind...

I made some test on my code. Here is a sample :
#include <iostream.h>
#include <memory.h>

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

Header.Signture =0x4241;
Header.FileSize =0x46454443;
Header.Reserved1 =0x4847;
Header.Reserved2 =0x4A49;
Header.ImgDataStartOff =0x4E4D4C4B;
Header.InfoHeaderSize =0x5251504F;
Header.Width =0x56555453;
Header.Height =0x5A595857;
Header.Planes =0x5C5B;
Header.NumOfPix =0x5E5D;
Header.Compression =0x6261605F;
Header.SizeOfImgData =0x66656463;
Header.HRes =0x6A696867;
Header.VRes =0x6E6D6C6B;
Header.NumOfCol =0x7271706F;
Header.NumOfImpCol =0x76757473;

union
{
char * CPtr;
short * SPtr;
} Ptr;

Ptr.SPtr = &Header.Signture;

cout<<"Size of Header = "<<sizeof(Header)<<endl;

for(int i=0; i<60; i++) cout<<i<<"\t"<<Ptr.CPtr[i]<<endl;

system("PAUSE>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.

I attached a doc file; have a look at it.
Attached Files
File Type: doc Forum.doc (43.5 KB, 15 views)
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,362
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 241
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: sizeof() for a Structure

 
0
  #8
Jul 29th, 2005
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).

[And I won't open attached .doc files.]
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC