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);

    }

    有时候,不小心知道了一些事,才发现自己所在乎的事是那么可笑。
  • 相关阅读:
    mysql只会使用到一个索引的原因
    SQL优化-三
    SQL优化-二
    SQL优化-一
    关于调和级数的证明
    SA 复习笔记
    点分治学习笔记
    FFT/NTT复习笔记&多项式&生成函数学习笔记Ⅰ
    python3+selenium webdriver实战应用篇-打造爱奇艺直播间机器人
    Photoshop如何修改(半)透明像素的RGB值
  • 原文地址:https://www.cnblogs.com/axjlxy/p/15012647.html
Copyright © 2011-2022 走看看