Struct varies in memory size?

前端 未结 2 1907
独厮守ぢ
独厮守ぢ 2020-12-02 00:42

Why is not 12 in the first case? Tested on: latest versions of gcc and clang, 64bit Linux

struct desc
{
    int** parts;
    int nr;
};

2条回答
  •  广开言路
    2020-12-02 01:18

    The compiler is allowed to add padding between struct members to make processing more efficient. This padding varies by platform, compiler version etc. It's one of the things that make sending structs over the network impossible.

    You can use offsetof to find out where exactly your compiler is adding paddings.

提交回复
热议问题