zoukankan      html  css  js  c++  java
  • C语言之——__attribute__

    __attribute__ ((packed)) 的作用就是告诉编译器取消结构在编译过程中的优化对齐,按照实际占用字节数进行对齐,是GCC特有的语法。这个功能是跟操作系统没关系,跟编译器有关 。

     __attribute__((aligned(4)));设置4字节对齐方式,和#pragma pack(4) 效果一样

    可以参考:https://blog.csdn.net/zhangxiong2532/article/details/50826917

    #include <stdio.h>
    
    struct mystruct11 
    {
        int a;
        char b;
        short c;
    }__attribute__((packed));
    
    struct mystruct21 
    {
        char a;
        int b;
        short c;
    }__attribute__((packed));
    
    struct p  
    {  
        int a;  
        char b;  
        char c;  
    }__attribute__((aligned(4))) p1;
    
    
    
    struct q  
    {  
        int a;  
        char b;  
        struct p qn;  
        char c;  
    }__attribute__((aligned(8))) q2;
    
    int main(void)
    {
        printf("sizeof(struct mystruct11) = %d.
    ",sizeof(struct mystruct11));
        printf("sizeof(struct mystruct21) = %d.
    ",sizeof(struct mystruct21));
        /*
        两个不同结构体1字节对齐的结构
        struct mystruct11                         struct mystruct21
        1字节对齐                          1字节对齐
        4                                                   1
        1                                                   4
        2                                                   2
        */
        
        printf("sizeof(int):%d, sizeof(char)=%d
    ", sizeof(int), sizeof(char));
        printf("sizeof(p1):%d
    ", sizeof(p1));
    
        printf("sizeof(q2):%d
    ", sizeof(q2));
        
        return 0;
    }
    
    sizeof(struct mystruct11) = 8.
    sizeof(struct mystruct21) = 10.
    sizeof(int):4, sizeof(char)=1
    sizeof(p1):8
    sizeof(q2):24
    
    --------------------------------
    Process exited after 0.01857 seconds with return value 0
    请按任意键继续. . .
  • 相关阅读:
    JavaScript
    JavaScript
    目标
    pip安装问题:There was a problem confirming ssl certificate
    chromedriver,python国内镜像地址收集
    检查网络问题
    linux 三剑客
    常用linux命令
    KVM学习
    rsync笔记
  • 原文地址:https://www.cnblogs.com/CodeWorkerLiMing/p/12007460.html
Copyright © 2011-2022 走看看