473,327 Members | 1,936 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

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 1368
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Patricia | last post by:
First, I know the following code is bad, but it's from a library I have to use, and I can't change it. class A { // some primitive members }; class B { // some primitive members };
11
by: Marlene Stebbins | last post by:
Something very strange is going on here. I don't know if it's a C problem or an implementation problem. The program reads data from a file and loads it into two arrays. When xy, x, y, *xlist and...
6
by: leonecla | last post by:
Hi everybody, I'm facing a very very strange problem with a very very simple C program... My goal should be to write to a binary file some numbers (integers), each one represented as a sequence...
2
by: Bruno van Dooren | last post by:
Hi All, i have some (3) different weird pointer problems that have me stumped. i suspect that the compiler behavior is correct because gcc shows the same results. ...
6
by: Tom | last post by:
I try to write a simple tcp based job queue server. It's purpose is to get commands from a client and store them in a queue (STL). The server has a thread which checks periodically the queue,...
2
by: rkk | last post by:
Hi, My mergesort program is below. There is a small piece of logical error/bug in this code which I can't figure out, as a result the output array isn't completely sorted. Requesting your help...
4
by: shaanxxx | last post by:
I have following programme: #include<stdio.h> int main() { int i=1; char *ptr=(char *)&i; if (*ptr)
17
by: venkat | last post by:
Hi, I have written a program void main() { printf("%d %d\n", sizeof main, sizeof(main())); } in this program the output is 1 and 4,
8
by: FBM | last post by:
Hi there, I am puzzled with the behavior of my code.. I am working on a networking stuff, and debugging with eclipse (GNU gdb 6.6-debian).. The problem I am experiencing is the following: ...
6
by: Gernot Frisch | last post by:
Hi, the program below workes w/o problems on a GP2X and on the PC, but my PocketPC (using GCC 3.3.3) crashes. Very dissapointing, since I expect some speed boost from it. Thnak you for your...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.