Strange sizeof behavior

P

Peter

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;
 
R

Robert Gamble

Peter said:
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
 
G

gbostock

Peter said:
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.
 
J

J. J. Farrell

Peter said:
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.
 
J

Joe Wright

Peter said:
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.
 
S

Simon Biber

Joe said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top