博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
计算类的大小--字节对齐
阅读量:2386 次
发布时间:2019-05-10

本文共 540 字,大约阅读时间需要 1 分钟。

class A{        int a;        short b;        int c;        char d;};class B{        double a;        short b;        int c;        char d;};

在32位机器上用gcc编译以上代码,求sizeof(A),sizeof(B)分别是多少。

正确答案: C   你的答案: C (正确)

12 16
12 12
16 24
16 20

C
根据以下条件进行计算:
1、  结构体的大小等于结构体内最大成员大小的整数倍
2、  结构体内的成员的首地址相对于结构体首地址的偏移量是其类型大小的整数倍,比如说double型成员相对于结构体的首地址的地址偏移量应该是8的倍数。
3、  为了满足规则1和2编译器会在结构体成员之后进行字节填充!

A中,a占4个字节,b本应占2个字节,但由于c占4个字节,为了满足条件2,b多占用2个字节,为了满足条件1,d占用4个字节,一共16个字节。
B中,a占8个字节,b占2个字节,但由于c占4个字节,为了满足条件2,b多占用2个字节,
即abc共占用8+4+4=16个字节,
为了满足条件1,d将占用8个字节,一共24个字节。

转载地址:http://fkiab.baihongyu.com/

你可能感兴趣的文章
Having fun web crawling with phantomJs
查看>>
Web Development – Installing mod_security with OWASP
查看>>
toolsmith: Recon-ng
查看>>
BF and IA vulnerabilities in IBM Lotus Domino
查看>>
php分页代码
查看>>
Yii 分页方法总结
查看>>
Active Directory Password Hash Extraction
查看>>
ColdFusion 9 / 10 Remote Root xday
查看>>
Exim / Dovecot Command Execution
查看>>
sudo 1.8.3p1 Local Root
查看>>
TinyMCE Ajax File Manager suffers from a remote code execution vulnerability.
查看>>
SMF 2.0.4 PHP Code Injection
查看>>
Financial Cyber Security
查看>>
SAP SOAP RFC SXPG_COMMAND_EXECUTE Remote Command Execution
查看>>
SAP SOAP RFC SXPG_CALL_SYSTEM Remote Command Execution
查看>>
Kloxo 6.1.6 - Local Privilege Escalation
查看>>
开源大数据解决方案
查看>>
storm在淘宝
查看>>
专升本
查看>>
WordPress插件W3 Total Cache爆漏洞可获取密码数据库信息
查看>>