Thread: Anonymous struct padding

  1. #1
    Registered User
    Join Date
    Feb 2021
    Posts
    6

    Anonymous struct padding

    Code:
    #include <stdio.h>
    
    struct A {
        int i;
        char a, b, c;
    };
    
    struct B {
        struct {
            int i;
            char a, b, c;
        };
    
        char d;
    };
    
    struct C {
        int i;
        char a, b, c, d;
    };
    
    int main() {
        printf("sizeof(struct A) = %lu\n", sizeof(struct A));
        printf("sizeof(struct B) = %lu\n", sizeof(struct B));
        printf("sizeof(struct C) = %lu\n", sizeof(struct C));
    
        return 0;
    }
    Compiled and executed with both GCC and Clang. I expect that the `sizeof` structs A, B, and C are all exactly 8 bytes (on a 32 bits system). But `struct B` is 12 bytes. I know why it is 12 bytes. What I don't understand is why the anonymous struct in `struct B` is padded to 8 bytes. Surely the compiler can figure out that `struct B` is already aligned to 4 bytes? What's going on here?

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    505
    struct A has to be padded to 8 bytes so that it is correctly aligned when you set up an array of them. struct B contains an identical struct to A, plus anohter member. So it's 8 bytes, plus one byte padded to 4 bytes, = 12 bytes.

    The compiler isn't clever enough to realise that since the member struct of struct B is anonymous, it can never appear in any other context than struct B, abd that it would be possible to reduce padding a bit more aggressively. It's a rarely-encountered situation, and it would have to special-case it to make this optimisation work.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


  3. #3
    Registered User
    Join Date
    Feb 2021
    Posts
    6
    Thanks, Malcolm Mclean. It is a pity that this is merely an unimplemented optimization. Well, at least in these cases #pragma pack(1) can save me some bytes with no performance cost.

  4. #4
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Quote Originally Posted by CoiledAlizarine View Post
    Thanks, Malcolm Mclean. It is a pity that this is merely an unimplemented optimization. Well, at least in these cases #pragma pack(1) can save me some bytes with no performance cost.
    Reading/Writing unaligned data always has a performance penalties (specially on Intel/AMD processors).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 04-07-2014, 05:38 PM
  2. Strange struct padding?
    By cyberfish in forum C++ Programming
    Replies: 8
    Last Post: 07-12-2011, 09:42 PM
  3. <anonymous struct> sucks
    By RichSelian in forum C Programming
    Replies: 7
    Last Post: 04-04-2011, 12:56 PM
  4. struct padding question..
    By sanddune008 in forum C Programming
    Replies: 3
    Last Post: 07-15-2010, 03:48 PM

Tags for this Thread