博客
关于我
C结构体内存对齐细节
阅读量:226 次
发布时间:2019-03-01

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

内存对齐的细节和编译器实现有关,一般有三个原则:

1. 结构体变量的首地址能够被其最宽基本类型成员的大小所整除;

2. 结构体每个成员相对于结构体首地址的偏移量(offset)都是成员大小的整数倍,如果有需要,编译器会在成员之间加上填充字节;

3. 结构体的总大小为结构体中最宽基本类型成员大小的整数倍,如果有需要,编译器会在最末一个成员之后加上填充字节。

ex :

struct  st{        uint16_t id; //2字节宽    uint32_t value; //4字节宽    uint64_t time; //8字节宽}

内存空间占用情况: 共占用16字节空间宽度


ex :

struct Date{    char a; //1字节    int b; //4字节    int64_t c; //8字节    char d; //1字节};Data data[2][10];

内存空间占用情况: 共占用16字节空间宽度

如果Data首地址为X,则data[1][5].c的地址是X+368.

 

参考文献:

【1】程序员面试笔试真题与解析

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

你可能感兴趣的文章
no connection could be made because the target machine actively refused it.问题解决
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
查看>>
No module named 'crispy_forms'等使用pycharm开发
查看>>
No module named 'pandads'
查看>>
No module named cv2
查看>>
No module named tensorboard.main在安装tensorboardX的时候遇到的问题
查看>>
No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
查看>>
No new migrations found. Your system is up-to-date.
查看>>
No qualifying bean of type XXX found for dependency XXX.
查看>>
No qualifying bean of type ‘com.netflix.discovery.AbstractDiscoveryClientOptionalArgs<?>‘ available
查看>>
No resource identifier found for attribute 'srcCompat' in package的解决办法
查看>>
no session found for current thread
查看>>
No static resource favicon.ico.
查看>>
no such file or directory AndroidManifest.xml
查看>>
No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
查看>>
NO.23 ZenTaoPHP目录结构
查看>>