麻烦懂C的个解释下!

efsnake 2008-08-27 01:13:16
#ifndef av_const
#if defined(__GNUC__) && (__GNUC__ > 2 || __GNUC__ == 2 && __GNUC_MINOR__ > 5)
# define av_const __attribute__((const))
#else
# define av_const
#endif
#endif

下面这2行到底定义了个什么东西?
# define av_const __attribute__((const))
# define av_const

还有:
typedef struct AVRational{
int num; ///< numerator
int den; ///< denominator
} AVRational;

AVRational av_mul_q(AVRational b, AVRational c) av_const;
AVRational av_div_q(AVRational b, AVRational c) av_const;
上面这2个函数定义的后面的av_const(就是上面定义的)表示什么?这个函数是返回了这个结构还是这个结构的指针?

谢谢!
...全文
88 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
jxw1987628 2008-08-27
  • 打赏
  • 举报
回复
下面的typedef是定义了1个结构体
TripH0101 2008-08-27
  • 打赏
  • 举报
回复
__attribute__ const
  该属性只能用于带有数值类型参数的函数上。当重复调用带有数值参数的函数时,由于返回值是相同的,所以此时编译器能够进行优化处理,除第一次需要运算外,其他只需要返回第一次的结果就能够了,进而能够提高效率。该属性主要适用于没有静态状态(static state)和副作用的一些函数,并且返回值仅仅依赖输入的参数。
TripH0101 2008-08-27
  • 打赏
  • 举报
回复
This attribute marks the function as considering only its numeric parameters. This is mainly intended for the compiler to optimize away repeated calls to a function that the compiler knows will return the same value repeatedly. It applies mostly to math functions that have no static state or side effects, and whose return is solely determined by the inputs.

In this highly-contrived example, the compiler normally must call the square() function in every loop even though we know that it's going to return the same value each time:

extern int square(int n) __attribute__((const));

...
for (i = 0; i < 100; i++ )
{
total += square(5) + i;
}
By adding __attribute__((const)), the compiler can choose to call the function just once and cache the return value.

In virtually every case, const can't be used on functions that take pointers, because the function is not considering just the function parameters but also the data the parameters point to, and it will almost certainly break the code very badly in ways that will be nearly impossible to track down.

Furthermore, the functions so tagged cannot have any side effects or static state, so things like getchar() or time() would behave very poorly under these circumstances.

13,825

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧