Strange struct padding?

This is a discussion on Strange struct padding? within the C++ Programming forums, part of the General Programming Boards category; I understand that the standard doesn't specify padding etc etc, but this is a "real world" question. I have a ...

  1. #1
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183

    Strange struct padding?

    I understand that the standard doesn't specify padding etc etc, but this is a "real world" question.

    I have a struct
    Code:
    struct S {
            long long int a;
            int b;
            int c;
            char d;
            char e;
    };
    and I expect its size to be 18 bytes, since no padding is needed (ints already aligned to 4-bytes boundaries, and chars don't matter).

    But gcc 4.2.3 on x86-64 Linux gives me a size of 24. How does that work?

    *edit* is it so that if I stack them up in an array, the long long member will be aligned to 8-bytes boundaries? */edit*
    Last edited by cyberfish; 12-14-2008 at 12:45 AM.

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    edit: meeh, I am not sure will let someone else give a better answer
    Last edited by C_ntua; 12-14-2008 at 01:12 AM.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I believe your edit is the correct reason.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    Both chars are apparently padded. Makes sense, since otherwise the "e" member would not be aligned. And if the last member isn't 4 bytes, then arrays won't be aligned.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Structs are typically end-padded to the size divisible by the alignment requirements of the most demanding member. Your guess is exactly the correct reason.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    I see.

    Many thanks.

  7. #7
    Registered User
    Join Date
    Jul 2011
    Posts
    2
    Structure padding is done by the compilers and this depends on the architectures. Some architectures cannot access the data which will be stored on the odd addresses or they may find difficult to access it. This is the reason for padding extra bytes.
    Structure padding in C

  8. #8
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    Stop bumping 4 years old threads!! Look at the date of the last post.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,743
    Quote Originally Posted by cyberfish
    Stop bumping 4 years old threads!! Look at the date of the last post.
    Quoted For Truth

    *thread closed*
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 12-03-2008, 02:10 AM
  2. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  3. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  4. Search Engine - Binary Search Tree
    By Gecko2099 in forum C Programming
    Replies: 9
    Last Post: 04-17-2005, 02:56 PM
  5. Bi-Directional Linked Lists
    By Thantos in forum C Programming
    Replies: 6
    Last Post: 12-11-2003, 09:24 AM

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