468,456 Members | 1,840 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,456 developers. It's quick & easy.

Strange sizeof behavior

In my code, taking a "sizeof (DF_Header_Data_t)" on the structure below
returns 48 when it should be 44. Why?

typedef struct DF_Header_Data_t {
int fbNumEntries;
int fbNumFrequencies;
double fbLowestFreq;
double fbHighestFreq;
int iNumPolarPerFreq;
int iNumCosElPerPolar;
int iNumAzDegPerCosEl;
int iNumAntPerAzDeg;
int iFreqListOffset;
} DF_Header_Data_t;

Aug 17 '06 #1
5 1240
Peter wrote:
In my code, taking a "sizeof (DF_Header_Data_t)" on the structure below
returns 48 when it should be 44. Why?

typedef struct DF_Header_Data_t {
int fbNumEntries;
int fbNumFrequencies;
double fbLowestFreq;
double fbHighestFreq;
int iNumPolarPerFreq;
int iNumCosElPerPolar;
int iNumAzDegPerCosEl;
int iNumAntPerAzDeg;
int iFreqListOffset;
} DF_Header_Data_t;
<http://c-faq.com/Question 2.13.

Robert Gamble

Aug 17 '06 #2

Peter wrote:
In my code, taking a "sizeof (DF_Header_Data_t)" on the structure below
returns 48 when it should be 44. Why?

typedef struct DF_Header_Data_t {
int fbNumEntries;
int fbNumFrequencies;
double fbLowestFreq;
double fbHighestFreq;
int iNumPolarPerFreq;
int iNumCosElPerPolar;
int iNumAzDegPerCosEl;
int iNumAntPerAzDeg;
int iFreqListOffset;
} DF_Header_Data_t;
Robert Gamble provided a link; the short answer is byte alignment. The
real question is why do you care? As long as you can use sizeof, you
get the size needed to hold it. The computer knows, so you don't need
to know.

Aug 17 '06 #3

Peter wrote:
In my code, taking a "sizeof (DF_Header_Data_t)" on the structure below
returns 48 when it should be 44. Why?

typedef struct DF_Header_Data_t {
int fbNumEntries;
int fbNumFrequencies;
double fbLowestFreq;
double fbHighestFreq;
int iNumPolarPerFreq;
int iNumCosElPerPolar;
int iNumAzDegPerCosEl;
int iNumAntPerAzDeg;
int iFreqListOffset;
} DF_Header_Data_t;
What makes you think it should be 44? The compiler can space the
structure members out as much as it likes, for whatever reason it wants
to.

Aug 17 '06 #4
Peter wrote:
In my code, taking a "sizeof (DF_Header_Data_t)" on the structure below
returns 48 when it should be 44. Why?

typedef struct DF_Header_Data_t {
int fbNumEntries;
int fbNumFrequencies;
double fbLowestFreq;
double fbHighestFreq;
int iNumPolarPerFreq;
int iNumCosElPerPolar;
int iNumAzDegPerCosEl;
int iNumAntPerAzDeg;
int iFreqListOffset;
} DF_Header_Data_t;
For what it's worth, gcc here reports 44.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Aug 17 '06 #5
Joe Wright wrote:
Peter wrote:
>In my code, taking a "sizeof (DF_Header_Data_t)" on the structure below
returns 48 when it should be 44. Why?

typedef struct DF_Header_Data_t {
int fbNumEntries;
int fbNumFrequencies;
double fbLowestFreq;
double fbHighestFreq;
int iNumPolarPerFreq;
int iNumCosElPerPolar;
int iNumAzDegPerCosEl;
int iNumAntPerAzDeg;
int iFreqListOffset;
} DF_Header_Data_t;
For what it's worth, gcc here reports 44.
DJGPP gcc reports 44, but Mingw32, Cygwin, Linux and Solaris gcc all
report 48. Solaris cc also reports 48. Microsoft cl, Borland bcc32 and
lcc-win32 lc compilers also report 48.

The compilers that report 48 bytes all placed 4 bytes padding at the end
of the struct. This would be so that the next struct in an array would
have 8 byte alignment for the double members.

I also tried on Turbo C 2.01, and it reports 30 bytes. That was achieved
with 2 bytes for int and 8 bytes for double, and no padding.

Unfortunately I don't have access to any more unusual platforms.

--
Simon.
Aug 18 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

13 posts views Thread by Patricia | last post: by
11 posts views Thread by Marlene Stebbins | last post: by
2 posts views Thread by Bruno van Dooren | last post: by
6 posts views Thread by Tom | last post: by
2 posts views Thread by rkk | last post: by
4 posts views Thread by shaanxxx | last post: by
17 posts views Thread by venkat | last post: by
6 posts views Thread by Gernot Frisch | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by subhajit12345 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.