[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

Beginner C question

Der Engel

4/14/2011 1:23:00 AM

Hi,

Why does the following code returns 28 in a i386 machine and 32 in a
amd64 machine? I guess this is more of a implementation/machine
question.


#include <stdio.h>

struct flex
{
int count;
double average;
double scores[2];
};

int main(void)
{
printf("%zd\n", sizeof (struct flex));
return 0;
}
19 Answers

Der Engel

4/14/2011 1:24:00 AM

0

On Apr 13, 8:22 pm, Der Engel <engels...@gmail.com> wrote:
> Hi,
>
> Why does the following code returns 28 in a i386 machine and 32 in a
> amd64 machine? I guess this is more of a implementation/machine
> question.
>
> #include <stdio.h>
>
> struct flex
> {
>     int count;
>     double average;
>     double scores[2];
>
> };
>
> int main(void)
> {
>     printf("%zd\n", sizeof (struct flex));
>     return 0;
>
> }

I must have said prints not returns.

Thanks

Ian Collins

4/14/2011 1:28:00 AM

0

On 04/14/11 01:22 PM, Der Engel wrote:
> Hi,
>
> Why does the following code returns 28 in a i386 machine and 32 in a
> amd64 machine? I guess this is more of a implementation/machine
> question.
>
>
> #include<stdio.h>
>
> struct flex
> {
> int count;
> double average;
> double scores[2];
> };
>
> int main(void)
> {
> printf("%zd\n", sizeof (struct flex));
> return 0;
> }

Alignment requirement of double (4 bytes on one, 8 on the other).

--
Ian Collins

China Blue Veins

4/14/2011 1:37:00 AM

0

In article <4cfd6690-b0a0-4ecc-ac59-fc56863d67b9@d12g2000vbz.googlegroups.com>,
Der Engel <engelster@gmail.com> wrote:

> Hi,
>
> Why does the following code returns 28 in a i386 machine and 32 in a
> amd64 machine? I guess this is more of a implementation/machine
> question.
>
>
> #include <stdio.h>
>
> struct flex
> {
> int count;
> double average;
> double scores[2];
> };
>
> int main(void)
> {
> printf("%zd\n", sizeof (struct flex));
> return 0;
> }

You can check sizeof(int) and sizeof(double).

sizeof(double) is likely to be 8, and size(int) is likely to be 4 or 8. If 4,
the minimum size is 4+8+2*8 = 28; if 8, 8+8+2*8 = 32. Also there may be
alignment issue where it's possible amd64 sizeof(int)=4 but it wants fields (or
at least double fields) on an 8 byte boundary, so it inserts 4 extra bytes of
padding to align the fields.

Unless you have a real need to worry about it, don't. Allocation size is usually
irrelevant with virtual memory.

--
Damn the living - It's a lovely life. I'm whoever you want me to be.
Silver silverware - Where is the love? At least I can stay in character.
Oval swimming pool - Where is the love? Annoying Usenet one post at a time.
Damn the living - It's a lovely life. I am in the Nile.

Martin Ambuhl

4/14/2011 3:34:00 AM

0

On 4/13/2011 9:22 PM, Der Engel wrote:
> Hi,
>
> Why does the following code returns 28 in a i386 machine and 32 in a
> amd64 machine? I guess this is more of a implementation/machine
> question.

There are at least three implementation specific variables here:
a) the size of an int
b) the size of a double
c) the amount of padding within the struct

One possibility consistent with your output is that
sizeof(double) = 8
sizeof(int) = 4
in each implementation, but the amd64 implementation has 4 bytes of
padding between count and average, presumably for alignment reasons.

Another possibility is
sizeof(double) = 8
in each implementation, but
sizeof(int) = 4 in the i386 case and
sizeof(int) = 8 in the amd64 case.

There are more possibilities. If this is actually a burning issue for
you, ask in a news group for your implementation. Any code that depends
of the sizeof(struct flex) is not portable.

> #include<stdio.h>
>
> struct flex
> {
> int count;
> double average;
> double scores[2];
> };
>
> int main(void)
> {
> printf("%zd\n", sizeof (struct flex));
> return 0;
> }

John Doe

4/14/2011 9:09:00 AM

0

On Wed, 13 Apr 2011 18:22:51 -0700, Der Engel wrote:

> Why does the following code returns 28 in a i386 machine and 32 in a
> amd64 machine?

It's almost certainly because the 64-bit system aligns doubles to an
8-byte (64-bit) boundary while the 32-bit system aligns them to a 4-byte
(32-bit) boundary, meaning that the 64-bit system has 4 bytes of padding
between "count" and "average".

There are other possible explanations, but the above is 99% likely to be
what's actually happening.

You can confirm this with e.g.:

struct flex f;
printf("%d\n", (int)((char*)f.average - (char*)f.count));

> I guess this is more of a implementation/machine question.

Yes. The C language doesn't dictate such details, allowing each
implementation to do whatever is most efficient for the hardware.

Keith Thompson

4/14/2011 3:16:00 PM

0

Der Engel <engelster@gmail.com> writes:
> Why does the following code returns 28 in a i386 machine and 32 in a
> amd64 machine? I guess this is more of a implementation/machine
> question.
>
>
> #include <stdio.h>
>
> struct flex
> {
> int count;
> double average;
> double scores[2];
> };
>
> int main(void)
> {
> printf("%zd\n", sizeof (struct flex));
> return 0;
> }

Try this; it shows the sizes and offsets of all the members of the
struct.

Note that you should be using "%zu", not "%zd", since size_t is an
unsigned type.

#include <stdio.h>
#include <stddef.h>

struct flex
{
int count;
double average;
double scores[2];
};

int main(void)
{
struct flex f;
printf("sizeof (struct flex) = %zu\n", sizeof (struct flex));
printf("sizeof f = %zu (should be the same)\n", sizeof f);
printf("f.count is %2zu bytes at offset %2zu\n",
sizeof f.count, offsetof(struct flex, count));
printf("f.average is %2zu bytes at offset %2zu\n",
sizeof f.average, offsetof(struct flex, average));
printf("f.scores is %2zu bytes at offset %2zu\n",
sizeof f.scores, offsetof(struct flex, scores));
printf("f.scores[0] is %2zu bytes at offset %2zu\n",
sizeof f.scores[0], offsetof(struct flex, scores[0]));
printf("f.scores[1] is %2zu bytes at offset %2zu\n",
sizeof f.scores[1], offsetof(struct flex, scores[1]));
return 0;
}

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.ne...
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Der Engel

4/14/2011 5:41:00 PM

0

On Apr 14, 10:15 am, Keith Thompson <ks...@mib.org> wrote:
> Der Engel <engels...@gmail.com> writes:
> > Why does the following code returns 28 in a i386 machine and 32 in a
> > amd64 machine? I guess this is more of a implementation/machine
> > question.
>
> > #include <stdio.h>
>
> > struct flex
> > {
> >     int count;
> >     double average;
> >     double scores[2];
> > };
>
> > int main(void)
> > {
> >     printf("%zd\n", sizeof (struct flex));
> >     return 0;
> > }
>
> Try this; it shows the sizes and offsets of all the members of the
> struct.
>
> Note that you should be using "%zu", not "%zd", since size_t is an
> unsigned type.
>
> #include <stdio.h>
> #include <stddef.h>
>
> struct flex
> {
>     int count;
>     double average;
>     double scores[2];
>
> };
>
> int main(void)
> {
>     struct flex f;
>     printf("sizeof (struct flex) = %zu\n", sizeof (struct flex));
>     printf("sizeof f = %zu (should be the same)\n", sizeof f);
>     printf("f.count     is %2zu bytes at offset %2zu\n",
>            sizeof f.count, offsetof(struct flex, count));
>     printf("f.average   is %2zu bytes at offset %2zu\n",
>            sizeof f.average, offsetof(struct flex, average));
>     printf("f.scores    is %2zu bytes at offset %2zu\n",
>            sizeof f.scores, offsetof(struct flex, scores));
>     printf("f.scores[0] is %2zu bytes at offset %2zu\n",
>            sizeof f.scores[0], offsetof(struct flex, scores[0]));
>     printf("f.scores[1] is %2zu bytes at offset %2zu\n",
>            sizeof f.scores[1], offsetof(struct flex, scores[1]));
>     return 0;
>
> }
>
> --
> Keith Thompson (The_Other_Keith) ks...@mib.org  <http://www.ghoti.ne...
> Nokia
> "We must do something.  This is something.  Therefore, we must do this."
>     -- Antony Jay and Jonathan Lynn, "Yes Minister"- Hide quoted text -
>
> - Show quoted text -

Thank you all for you answers!

Michael Press

4/17/2011 2:54:00 AM

0

In article
<4cfd6690-b0a0-4ecc-ac59-fc56863d67b9@d12g2000vbz.googlegroups.com>,
Der Engel <engelster@gmail.com> wrote:

> Hi,
>
> Why does the following code returns 28 in a i386 machine and 32 in a
> amd64 machine? I guess this is more of a implementation/machine
> question.
>
>
> #include <stdio.h>
>
> struct flex
> {
> int count;
> double average;
> double scores[2];
> };
>
> int main(void)
> {
> printf("%zd\n", sizeof (struct flex));
> return 0;
> }

I guess that the amd64 machine has sizeof(int) == 8,
and the i386 machine has sizeof(int) == 4.

--
Michael Press

Ian Collins

4/17/2011 2:58:00 AM

0

On 04/17/11 02:54 PM, Michael Press wrote:
> In article
> <4cfd6690-b0a0-4ecc-ac59-fc56863d67b9@d12g2000vbz.googlegroups.com>,
> Der Engel<engelster@gmail.com> wrote:
>
>> Hi,
>>
>> Why does the following code returns 28 in a i386 machine and 32 in a
>> amd64 machine? I guess this is more of a implementation/machine
>> question.
>>
>>
>> #include<stdio.h>
>>
>> struct flex
>> {
>> int count;
>> double average;
>> double scores[2];
>> };
>>
>> int main(void)
>> {
>> printf("%zd\n", sizeof (struct flex));
>> return 0;
>> }
>
> I guess that the amd64 machine has sizeof(int) == 8,
> and the i386 machine has sizeof(int) == 4.

You are probably wrong.

--
Ian Collins

Michael Press

4/17/2011 3:09:00 AM

0

In article <90v3a6FpdmU4@mid.individual.net>,
Ian Collins <ian-news@hotmail.com> wrote:

> On 04/17/11 02:54 PM, Michael Press wrote:
> > In article
> > <4cfd6690-b0a0-4ecc-ac59-fc56863d67b9@d12g2000vbz.googlegroups.com>,
> > Der Engel<engelster@gmail.com> wrote:
> >
> >> Hi,
> >>
> >> Why does the following code returns 28 in a i386 machine and 32 in a
> >> amd64 machine? I guess this is more of a implementation/machine
> >> question.
> >>
> >>
> >> #include<stdio.h>
> >>
> >> struct flex
> >> {
> >> int count;
> >> double average;
> >> double scores[2];
> >> };
> >>
> >> int main(void)
> >> {
> >> printf("%zd\n", sizeof (struct flex));
> >> return 0;
> >> }
> >
> > I guess that the amd64 machine has sizeof(int) == 8,
> > and the i386 machine has sizeof(int) == 4.
>
> You are probably wrong.

Would not be the first time.

--
Michael Press