Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Beginner C question

Reply
Thread Tools

Beginner C question

 
 
Der Engel
Guest
Posts: n/a
 
      04-14-2011
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;
}
 
Reply With Quote
 
 
 
 
Der Engel
Guest
Posts: n/a
 
      04-14-2011
On Apr 13, 8:22*pm, Der Engel <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      04-14-2011
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
 
Reply With Quote
 
Nobody
Guest
Posts: n/a
 
      04-14-2011
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.

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      04-14-2011
Der Engel <(E-Mail Removed)> 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) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Der Engel
Guest
Posts: n/a
 
      04-14-2011
On Apr 14, 10:15*am, Keith Thompson <(E-Mail Removed)> wrote:
> Der Engel <(E-Mail Removed)> 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) (E-Mail Removed) *<http://www.ghoti.net/~kst>
> 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!
 
Reply With Quote
 
Michael Press
Guest
Posts: n/a
 
      04-17-2011
In article
<(E-Mail Removed)>,
Der Engel <(E-Mail Removed)> 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
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      04-17-2011
On 04/17/11 02:54 PM, Michael Press wrote:
> In article
> <(E-Mail Removed)>,
> Der Engel<(E-Mail Removed)> 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
 
Reply With Quote
 
Michael Press
Guest
Posts: n/a
 
      04-17-2011
In article <(E-Mail Removed)>,
Ian Collins <(E-Mail Removed)> wrote:

> On 04/17/11 02:54 PM, Michael Press wrote:
> > In article
> > <(E-Mail Removed)>,
> > Der Engel<(E-Mail Removed)> 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
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      04-17-2011
On 04/17/11 03:08 PM, Michael Press wrote:
> In article<(E-Mail Removed)>,
> Ian Collins<(E-Mail Removed)> wrote:
>
>> On 04/17/11 02:54 PM, Michael Press wrote:
>>>
>>> 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.




I know the feeling!

--
Ian Collins
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Beginner's Beginner william nelson Ruby 7 04-11-2011 11:23 PM
beginner's question.. Scott Needham Wireless Networking 4 09-06-2005 12:52 AM
No Class at ALL!!! beginner/beginner question =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?= ASP .Net 7 02-03-2005 02:47 PM
Tutorial for beginner/ Tutorial voor beginner Rensjuh C++ 7 09-02-2004 12:41 AM
Beginner question: What trigs processes Jerker Hammarberg VHDL 16 07-22-2003 03:58 PM



Advertisments