Der Engel said:
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.
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 address 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 -