We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

A struct like this, find size....

chunich
chunich asked
on
Medium Priority
362 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
Comment
Watch Question

Harisha M GEngineer
CERTIFIED EXPERT

Commented:
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
Harisha M GEngineer
CERTIFIED EXPERT

Commented:
Not nearest... I can say "round up"
Harisha M GEngineer
CERTIFIED EXPERT

Commented:
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

Author

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.
Harisha M GEngineer
CERTIFIED EXPERT

Commented:
> 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
Top Expert 2005

Commented:

Author

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.
Engineer
CERTIFIED EXPERT
Commented:
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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Harisha M GEngineer
CERTIFIED EXPERT

Commented:
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   _   _   _   _

Author

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!
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.