结构在内存大小而异? [英] Struct varies in memory size?

查看:105
本文介绍了结构在内存大小而异?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么不在第一壳体12?
测试在:gcc和铿锵的最新版本,64位的Linux

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

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

的sizeof(DESC);输出:16

struct desc
{
    int** parts;
};

的sizeof(DESC);输出:8

struct desc
{
    int nr;
};

的sizeof(DESC);输出:4

推荐答案

编译器允许结构成员之间添加填充物,使处理更高效。这种填充因平台而异,编译器版本等,这是的,使发送结构在网络上的一件事是不可能的。

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.

您可以使用 offsetof 来找出究竟你的编译器是增加垫。

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

这篇关于结构在内存大小而异?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆