sizeof struct

This is a discussion on sizeof struct within the C Programming forums, part of the General Programming Boards category; I'm running the following prog : Code: #include <stdio.h> struct customer { short number; // 1: int postcode; // 2: ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    27

    sizeof struct

    I'm running the following prog :

    Code:
    #include <stdio.h>
    
    
    struct customer {
          short number;      //   1:
          int postcode;       //   2:
    };
    
    int main() {
        printf("%d\n",sizeof(customer));
    }
    If I run it with line 1 in comments the output is 4.OK for that.
    If I run it with line 2 in comments the output is 2.OK for that too.
    But if i run as it is above the outout is 8.Why is that?

    Thank you in advance.
    Last edited by k_w_s_t_a_s; 05-10-2003 at 03:19 PM.

  2. #2
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    sizeof(struct customer);
    or typedef and you could use just customer.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    27
    Basically i did it in C++ that's why I wrote :

    printf("%d\n",sizeof(customer));

    But even with printf("%d\n",sizeof(struct customer));
    it prints 8.Why?

  4. #4
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    and what you expect to print? sizeof returns you the size of this struct, not how many elements you have on it. So 8 is perfect, look:

    Code:
    struct customer {
          short number;      //   sizeof(short) == 4!
          int postcode;       //   sizeof(int) == 4!
          //4+4 = 8
    };

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    27
    The sizeof(short) is 2 not 4.

  6. #6
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    well, it's really machine dependent... but in your case if it's 2, and you're still receiving 8, strange.

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    27
    when i run :
    printf("%d",sizeof(short));

    it prints 2 but when i run the program with the struct it prints 8.
    Last edited by k_w_s_t_a_s; 05-10-2003 at 04:30 PM.

  8. #8
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    169
    Its to do with struct member alignment. Code can be compiled with different alignments either 1, 2, 4, 8, 16 bytes (MS VC++ 6), 8 is the default setting. I compiled the code different alignments:

    1 byte - sizeof = 6
    2 byte - sizeof = 6
    4 byte - sizeof = 8
    8 byte - sizeof = 8
    16 byte - sizeof = 8
    Last edited by Scarlet7; 05-10-2003 at 04:47 PM.

  9. #9
    Registered User devil@work's Avatar
    Join Date
    Mar 2003
    Posts
    33
    did you try to use another compiler and see what happens maybe you compiler add something to optimize the code?

  10. #10
    Registered User
    Join Date
    May 2003
    Posts
    4
    The compiler had to add a couple bytes because of alignment restrictions. If you need 6 bytes exactly, try looking for some "packed" pragma's, or compiler-specific keywords used after "struct". Why is this a problem in your case? There might be an easier work-around.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  2. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 02:04 PM
  3. Replies: 10
    Last Post: 05-19-2006, 12:23 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  5. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21