C99的'限制'关键字的现实使用情况如何? [英] Realistic usage of the C99 'restrict' keyword?

查看:94
本文介绍了C99的'限制'关键字的现实使用情况如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是通过一些文档和问题/答案浏览并看到它提及。我读了简要介绍,指出这将是基本上是从该指针不会被用来指向其它地方的程序员的承诺。

I was browsing through some documentation and questions/answers and saw it mentioned. I read a brief description, stating that it would be basically a promise from the programmer that the pointer won't be used to point somewhere else.

任何人都可以提供一些现实的情况下,它的价值实际使用呢?

Can anyone offer some realistic cases where its worth actually using this?

推荐答案

限制说,指针是访问底层对象的唯一的事。它消除了潜在的指针别名,由编译器实现更好的优化。

restrict says that the pointer is the only thing that accesses the underlying object. It eliminates the potential for pointer aliasing, enabling better optimization by the compiler.

例如,假设我有,可以成倍的数字矢量内存专门指令的机器,我有以下的code:

For instance, suppose I have a machine with specialized instructions that can multiply vectors of numbers in memory, and I have the following code:

void MultiplyArrays(int* dest, int* src1, int* src2, int n)
{
    for(int i = 0; i < n; i++)
    {
        dest[i] = src1[i]*src2[i];
    }
}

编译器需要妥善处理如果dest,src1的,和SRC2重叠,这意味着它必须在同一时间做一次乘法,从开始到结束。通过让限制,编译器是免费的这个code优化使用向量指令。

The compiler needs to properly handle if dest, src1, and src2 overlap, meaning it must do one multiplication at a time, from start to the end. By having restrict, the compiler is free to optimize this code to using the vector instructions.

编辑:维基百科对条目限制,与另外一个例子,这里

Wikipedia has an entry on restrict, with another example, here.

这篇关于C99的'限制'关键字的现实使用情况如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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