Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

A struct like this, find size....

Posted on 2006-04-13
10
Medium Priority
?
341 Views
Last Modified: 2010-04-15
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
0
Comment
Question by:chunich
  • 6
  • 3
10 Comments
 
LVL 37

Expert Comment

by:Harisha M G
ID: 16448599
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
 
LVL 37

Expert Comment

by:Harisha M G
ID: 16448606
Not nearest... I can say "round up"
0
 
LVL 37

Expert Comment

by:Harisha M G
ID: 16448626
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:chunich
ID: 16448710
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
 
LVL 37

Expert Comment

by:Harisha M G
ID: 16448763
> 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
 
LVL 23

Expert Comment

by:brettmjohnson
ID: 16448792
0
 

Author Comment

by:chunich
ID: 16449018
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
 
LVL 37

Accepted Solution

by:
Harisha M G earned 200 total points
ID: 16449114
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
 
LVL 37

Expert Comment

by:Harisha M G
ID: 16449137
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
 

Author Comment

by:chunich
ID: 16449182
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
Suggested Courses

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question