Forum Discussion

ute1405's avatar
ute1405
Copper Contributor
Feb 03, 2024

Why the size of type Bar is 3 not 4 in MSVC?

#include <iostream>
using std::cout;

struct Bar {
    char name[3];
    struct Foo {
        int a;
        int b;
    };
};

int main()
{
    cout << sizeof(Bar);
    return 0;
}

Usually it aligns so it's multiple of sizeof(int).

2 Replies

  • FrancisMoore's avatar
    FrancisMoore
    Iron Contributor

    Please note: In the `Bar` structure, there are no members requiring alignment after `char name[3]`, so its size is 3 bytes; it has not been padded to 4 bytes. Simply amend this.

     

     

  • NicholasWhite's avatar
    NicholasWhite
    Copper Contributor

    Since there are no members following `char name[3]` in the `Bar` structure, it does not need to be aligned; therefore, its size is 3 bytes, and it is not padded to 4 bytes.