By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,590 Members | 1,140 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,590 IT Pros & Developers. It's quick & easy.

Strange sizeof behavior

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
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

P: n/a

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

P: n/a

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

P: n/a
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

P: n/a
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.