__attribute __((常量))与__attribute __在GNU C((折纯)) [英] __attribute__((const)) vs __attribute__((pure)) in GNU C

查看:505
本文介绍了__attribute __((常量))与__attribute __在GNU C((折纯))的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

之间有什么的区别__在GNU C属性__((常量)) __属性__((折纯))

  __ __属性((常量))INT F(){
    / * ... * /
    返回4;
}

VS

  __ __属性((折纯))INT F(){
    / * ... * /
    返回4;
}


解决方案

从的为ARM编译器(这是基于GCC)文档:


  

__ __属性((折纯))函数属性结果
  许多功能都只是返回一个值没有影响,它们的返回值仅取决于参数和全局变量。这种功能可以受到数据流分析,并可能被淘汰。


  
  

__ __属性((常量))函数属性结果
  很多函数只检查传递给它们的参数,并有除返回值没​​有影响。这比更严格的阶级属性__ __((折纯)),因为一个功能是不允许读取全局内存。如果一个函数是已知的唯一对其参数进行操作,然后它可以进行公共子前pression消除和循环优化。


所以,TL; DR: __属性__((常量))相同 __属性__((折纯))但没有全局变量的任何访问。

What is the difference between __attribute__((const)) and __attribute__((pure)) in GNU C?

__attribute__((const)) int f() {
    /* ... */
    return 4;
}

vs

__attribute__((pure)) int f() {
    /* ... */
    return 4;
}

解决方案

From the documentation for the ARM compiler (which is based on gcc):

__attribute__((pure)) function attribute
Many functions have no effects except to return a value, and their return value depends only on the parameters and global variables. Functions of this kind can be subject to data flow analysis and might be eliminated.

__attribute__((const)) function attribute
Many functions examine only the arguments passed to them, and have no effects except for the return value. This is a much stricter class than __attribute__((pure)), because a function is not permitted to read global memory. If a function is known to operate only on its arguments then it can be subject to common sub-expression elimination and loop optimizations.

So, TL;DR: __attribute__((const)) is the same as __attribute__((pure)) but without any access to global variables.

这篇关于__attribute __((常量))与__attribute __在GNU C((折纯))的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆