how to calculate size of a struct?

Discussion in 'C' started by vql, Sep 27, 2007.

  1. vql

    vql New Member

    Joined:
    Sep 27, 2007
    Messages:
    18
    Likes Received:
    0
    Trophy Points:
    0
    I declare a struct:

    struct ABC
    {
    int a;
    int b;
    char c;
    in d;
    }

    I use VC++ compiler.
    In main, I call function sizeof(ABC) and it returns 16. Why? Because I think sizeof(ABC) = sizeof(int) + sizeof(int) + sizeof(char) + sizeof(int) = 4+4+1+4 = 13.

    What happened?
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    It returns 16 because of the compiler using the packaging format.

    I would try to explain.

    When you have data type which holds the maximum chunk of byte it allocates the other smaller byte as a multiple of the highest possible bytes taken for optimization of the memory and nowadays memory is quite cheap.

    Try putting a float and or a double and see how the things change.
     
  3. vql

    vql New Member

    Joined:
    Sep 27, 2007
    Messages:
    18
    Likes Received:
    0
    Trophy Points:
    0
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Its the class diagram option available in MS VC 2005
     
  5. vql

    vql New Member

    Joined:
    Sep 27, 2007
    Messages:
    18
    Likes Received:
    0
    Trophy Points:
    0
    With your answer, 3 remaining bytes will be used for what?

    With 16 bytes, you can tell me the position of struct's members. Example: a from byte 0->byte 3, ... Please continue to explain for me clearly.

    Why the compiler use this calculation way? Thanks.
     
  6. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
  7. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    You can choose to pack the struct so that it uses the number of bytes you think it should use, rather than aligning members on certain boundaries. In doing so you will cause a failure on systems that do not allow access on every byte boundary (Bus Fault). If you are not intimately familiar with your implementation, you will do well to accept the compiler's safer arrangement, despite the loss of a couple bytes of memory.
     
  8. vql

    vql New Member

    Joined:
    Sep 27, 2007
    Messages:
    18
    Likes Received:
    0
    Trophy Points:
    0
    I can't understand clearly. I am reading about option /Zp in Visual C++ Compiler. Maybe I will give myseft some examples to understand about this. Thanks.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice