zoukankan      html  css  js  c++  java
  • c语言结构体对齐例子

    结构体里的变量8字节对齐     指的是相对于结构体首地址的偏移是8字节的整数倍

     

    ldrexd的指令   要求的是被操作的内存地址的8字节对齐

     

    p:0x56401b274264
    p->a:0x56401b274264
    p->b:0x56401b274268
    p->c:0x56401b27426c
    p->d:0x56401b274274
    p->e:0x56401b27427c
    p->f:0x56401b274284
    p->g:0x56401b274288
    a:0x7ffdfc13ee60
    b:0x7ffdfc13ee64
    c:0x7ffdfc13ee68
    d:0x7ffdfc13ee70
     cat attribute.c
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    struct attr {
    char a __attribute__((__aligned__(8)));
    int b ;
    double c __attribute__((__aligned__(8)));
    long d __attribute__((__aligned__(8)));

    char e __attribute__((__aligned__(2)));
    char f __attribute__((__aligned__(8)));
    char g __attribute__((__aligned__(4)));

    }__attribute__((__aligned__(8)));
    int main()
    {
    struct attr abc;
    char * tmpp = malloc(sizeof(struct attr)+100);
    struct attr *p =tmpp+4;
    printf("p:%p ",p);
    printf("p->a:%p ",&p->a);
    printf("p->b:%p ",&p->b);
    printf("p->c:%p ",&p->c);
    printf("p->d:%p ",&p->d);
    printf("p->e:%p ",&p->e);
    printf("p->f:%p ",&p->f);
    printf("p->g:%p ",&p->g);
    abc.a = 1;
    abc.b = 2;
    abc.c = 3;
    abc.d = 4;
    printf("a:%p ",&abc.a);
    printf("b:%p ",&abc.b);
    printf("c:%p ",&abc.c);
    printf("d:%p ",&abc.d);

    }

    有时候,不小心知道了一些事,才发现自己所在乎的事是那么可笑。
  • 相关阅读:
    轻量级锁和偏向锁等
    桥接模式
    适配器模式
    建造者模式
    2-工厂模式
    Swift
    给视图添加点击波纹效果swift5
    Xcode 支持真机版本路径
    22个常用开源库(most swift)
    Github上关于iOS的各种开源项目集合
  • 原文地址:https://www.cnblogs.com/axjlxy/p/15012647.html
Copyright © 2011-2022 走看看