Thread: Strange struct padding?

  1. #1
    Registered User
    Join Date
    Dec 2006

    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
    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
    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
    I believe your edit is the correct reason.

  4. #4
    Join Date
    Oct 2007
    Inside my computer
    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
    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!

    "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
    I see.

    Many thanks.

  7. #7
    Registered User
    Join Date
    Jul 2011
    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
    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
    Quote Originally Posted by cyberfish
    Stop bumping 4 years old threads!! Look at the date of the last post.
    Quoted For Truth

    *thread closed*
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
Website Security Test