• C

A struct like this, find size....

I have a C-struct like this, and when I ran it shows 16 for the comp in total.
10 chars = 10 bytes,
1 int = 4 bytes.

Total I expect is 14 bytes but it shows 16.
Where is the extra 2 coming from? For each structure itself?  Is it always 2 then?

struct computer{
  int  speed;
  char type[10];
} comp[] = {
            {400, "P"},
            {300, "E"},
            {200, "A"}
           };

printf( "%d\n", sizeof(int));
printf( "%d\n", sizeof(char));
printf( "%d\n", sizeof(*comp));
printf( "%d\n", sizeof(comp));


Output:
4
1
16
48
chunichAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Harisha M GEngineerCommented:
Hi, that is because of aligning.. you must be running this program in a 32 bit environment (4 byte)

So, the size is shown as the nearest multiple of 4

---
Harish
0
Harisha M GEngineerCommented:
Not nearest... I can say "round up"
0
Harisha M GEngineerCommented:
You can disable this feature in some compilers using "#pragma pack" directives..

http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=/com.ibm.vacpp6m.doc/compiler/ref/rnpgpack.htm
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

chunichAuthor Commented:
Aligning, so I modified it a little bit:

struct computer{
  char type[3];
} comp[] = {
            {"P"},
            {"E"},
            {"A"}
           };

printf( "%d\n", sizeof(*comp));
printf( "%d\n", sizeof(comp));

Output:
3
9



And I also tried this:
struct computer{
  int a; int b; int c; int d;
  char type[3];
} comp[] = {
            {1, 2, 3, 4, "P"},
            {1, 2, 3, 4, "E"},
            {1, 2, 3, 4, "A"}
           };

printf( "%d\n", sizeof(*comp));
printf( "%d\n", sizeof(comp));

Output:
20
60

So does alignment happens on when there is a mix of different types in a struct?
When I changed type[3] to type[2], output is still 20, 60.
0
Harisha M GEngineerCommented:
> So does alignment happens on when there is a mix of different types in a struct?

Yes. Consider the memory as an array of blocks of 4 bytes...

 __  __  __  __
|__|__|__|__|
|__|__|__|__|
|__|__|__|__|
|__|__|__|__|
|__|__|__|__|
|__|__|__|__|
...

Now, if you store 3 characters, then it is store as

C  C  C

But if you store one integer also along with those 3 characters..

C  C  C   I
I   I   I  

This is not so good while fetching the data (since data is read in terms of words, and it requires 2 words to be read to access a single integer. So it will be stored as


C  C  C  
I   I   I   I  

so that accessing will be easier. As a result, you will get the size to be 8 bytes
0
brettmjohnsonCommented:
0
chunichAuthor Commented:
Thanks!! I think I got the idea but still haven't figured out the details.

Rules:
    *  Single byte numbers can be aligned at any address
    * Two byte numbers should be aligned to a two byte boundary
    * Four byte numbers should be aligned to a four byte boundary
    * Structures between 1 and 4 bytes of data should be padded so that the total structure is 4 bytes.
    * Structures between 5 and 8 bytes of data should be padded so that the total structure is 8 bytes.
    * Structures between 9 and 16 bytes of data should be padded so that the total structure is 16 bytes.
    * Structures greater than 16 bytes should be padded to 16 byte boundary.

struct computer{
  short a; // pad 0
  short b; // pad 0
  long  c; // pad 0
  char d[3]; // pad 1
  double e; // pad 0
  long f; // pad 0
  long g; // pad 0
  long h; // pad 0
} comp[] = {
            {1, 2, 3, "4", 5, 6, 7, 8}
           };

If I'm following the rules correctly, I was expecting 2 + 2 + 4 + 4 + 8 + 4 + 4 + 4 = 32 and the output is 40.
0
Harisha M GEngineerCommented:
Which OS are you using ? Which processor ?

After a bit of testing, I found that the data are stored in this manner.. which comes to 40 bytes

a   a   b   b   c   c   c    c
d   d   d
e   e   e   e   e   e   e   e
f    f    f    f   g   g   g   g
h   h   h   h
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Harisha M GEngineerCommented:
As you have guessed, blanks are there...

a   a   b   b   c   c   c    c
d   d   d   _   _   _   _   _
e   e   e   e   e   e   e   e
f    f    f    f   g   g   g   g
h   h   h   h   _   _   _   _
0
chunichAuthor Commented:
You're the man!
I think I got it now.
So it uses the longest length of the structure - double - as "standard".
if anything before "double" is shorter than 8 and can fit on a single line after padding,
they'll be squeezed onto the same line - just like aa bb and cccc.

I moved the variable h (h h h h) to before the double e,
the size is reduced to 32 since there's 4 words left with d d d the same word as h h h h

THANKS!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.