zoukankan      html  css  js  c++  java
  • __attribute__ ((attribute-list))

    http://blog.csdn.net/ithomer/article/details/6566739


    构造与析构:


    #include <stdio.h> #include <stdlib.h> static __attribute__((constructor)) void before() { printf("Hello"); } static __attribute__((destructor)) void after() { printf(" World! "); } int main(int args,char ** argv) { printf("main"); return EXIT_SUCCESS; } ~
    [root@workstation2017 ~]# ./test.out
    Hellomain World!

    优先级:

    [root@workstation2017 ~]# vi test.c
    
    #include <stdio.h>
    #include <stdlib.h>
    
    
    static  __attribute__((constructor(101))) void begin101()
    {
    
        printf("Hello-101
    ");
    }
    
    static  __attribute__((constructor(102))) void begin102()
    {
    
        printf("Hello-102
    ");
    }
    
    
    static  __attribute__((destructor(150))) void end101()
    {
        printf(" World!-101
    ");
    }
    
    static  __attribute__((destructor(151))) void end102()
    {
        printf(" World!-102
    ");
    }
    
    int main(int args,char ** argv)
    {
    
        return EXIT_SUCCESS;
    }
    [root@workstation2017 ~]# ./test.out
    Hello-101
    Hello-102
     World!-102
     World!-101

    对齐属性:

    __attrubte__ ((packed)) 的作用就是告诉编译器取消结构在编译过程中的优化对齐,按照实际占用字节数进行对齐

    [root@workstation2017 ~]# vi test.c
    
    #include <stdio.h>
    #include <stdlib.h>
    #define __u8    unsigned char
    #define __u16   unsigned short
    
    struct str_struct{
            __u8    a;
            __u8    b;
            __u8    c;
            __u16   d;
    } __attribute__ ((packed));
    
    typedef struct {
            __u8    a;
            __u8    b;
            __u8    c;
            __u16   d;
    } __attribute__ ((packed)) str;
    
    typedef struct {
            __u8    a;
            __u8    b;
            __u8    c;
            __u16   d;
    }str_temp __attribute__ ((packed));
    
    typedef struct {
            __u8    a;
            __u8    b;
            __u8    c;
            __u16   d;
    }str_nopacked;
    
    int main(void)
    {
            printf("sizeof str = %d
    ", sizeof(str));
            printf("sizeof str_struct = %d
    ", sizeof(struct str_struct));
            printf("sizeof str_temp = %d
    ", sizeof(str_temp));
            printf("sizeof str_nopacked = %d
    ", sizeof(str_nopacked));
            return 0;
    }
    [root@workstation2017 ~]# ./test.xx
    sizeof str = 5
    sizeof str_struct = 5
    sizeof str_temp = 6
    sizeof str_nopacked = 6

    变量占内存大小 x64 x32差别:

     变量的长度,在不同的系统之间会有差别,64位Linux系统和32位Linux系统中,几种常见C语言变 量的长度:
                  short    int    long    long long    ptr    time_t
    32位           2         4       4          8      4        4
    64位           2         4       8          8      8        8
  • 相关阅读:
    Unity 3(一):简介与示例
    MongoDB以Windows Service运行
    动态SQL中变量赋值
    网站发布IIS后堆栈追踪无法获取出错的行号
    GridView Postback后出错Operation is not valid due to the current state of the object.
    Visual Studio 2010 SP1 在线安装后,找到缓存在本地的临时文件以便下次离线安装
    SQL Server 问题之 排序规则(collation)冲突
    IIS 问题集锦
    linux下安装mysql(ubuntu0.16.04.1)
    apt-get update 系列作用
  • 原文地址:https://www.cnblogs.com/zengkefu/p/7290195.html
Copyright © 2011-2022 走看看