zoukankan      html  css  js  c++  java
  • C 语言的 GCC 扩展

    GNU 编译器(GCC)提供了很多 C 语言扩展,编译器会使用该信息生成更高效的机器代码。

    内联函数

    static inline __attribute__ ((always_inline)) int foo(void) { /* ... */ }


    避免内联

    __attribute__ ((noinline)) int foo(void) { /*...*/ }


    纯函数

    __attribute__ ((pure)) int foo(int val) { /* */ }

    返回值只受函数参数或 nonvolatile 全局变量影响。

    常函数

    __attribute__ ((const)) int foo(int val) { /*...*/ }

    常函数是一种严格的纯函数。常函数不能访问全局变量,参数不能是指针类型。因此,常函数的返回值只和值传递的参数值有关。

    没有返回值的函数

    __attribute__ ((noreturn)) void foo(int val) { /*...*/ }

    分配内存的函数

    __attribute__ ((malloc)) void *copy()
    {
       int size = 10;
       return malloc(10);
    }

    强制调用方检查返回值

     1 #include <stdio.h>
     2 
     3 __attribute__ ((warn_unused_result)) int foo()
     4 {
     5     return 0;
     6 }
     7 
     8 int main()
     9 {
    10     foo();
    11     return 0;
    12 }


    把函数标识为“Deprecated(已废弃)”

    __attribute__ ((deprecated)) void foo(void) { /*...*/ }


    把函数标识为已使用

    static __attribute__ ((used)) void foo(void) { /*...*/ }


    把函数或参数标识为未使用的

    int foo(int __attribute__ ((unused)) value) { /*...*/}


    对结构体进行紧凑存储(pack)

    struct __attribute__ ((packed)) foo { ... };

    packed 属性告诉编译器一个类型或变量应该在内存中紧凑存储,使用尽可能少的空间,可能不依赖对齐需求。如果在结构体(struct)或联合体(union)上指定该属性,就需要对所有变量进行紧凑存储。如果只是对某个变量指定该属性,就只会紧凑存储该特定变量。

  • 相关阅读:
    learning java ATW ScrollPane
    SQLSERVER2008R2正确使用索引
    SQL Server 数据操作
    jar war ear
    浅谈SQL Server中的三种物理连接操作
    SqlServer在视图上创建索引的条件
    Sqlserver 查看视图或者存储过程定义
    过滤器配置
    SpringMVC架构
    N+1问题其实应该叫做1+N 问题
  • 原文地址:https://www.cnblogs.com/jingyg/p/5398476.html
Copyright © 2011-2022 走看看