吾爱程序员:这里有好玩的游戏和软件
当前位置:首页C语言教程 → C中的restrict 关键字

C中的restrict 关键字

来源:网络 | 更新时间:2022-01-18 19:48:42
C 编程语言(99 标准之后)中,引入了一个新的关键字,称为restrict 。 
  • restrict 关键字主要用于指针声明中,作为指针的类型限定符。
  • 它没有添加任何新功能。这只是程序员告知编译器可以进行的优化的一种方式。
  • 当我们对指针 ptr 使用 restrict 时,它告诉编译器 ptr 是访问它所指向的对象的唯一方法,换句话说,没有其他指针指向同一个对象,即,restrict 关键字指定特定的指针参数确实不为其他任何别名,编译器不需要添加任何额外的检查。
  • 如果程序员使用restrict 关键字并违反上述条件,则结果是未定义的行为。
  • C++ 不支持restrict 。它是 C 唯一的关键字。
// C program to use restrict keyword.
#include <stdio.h>

// Note that the purpose of restrict is to
// show only syntax. It doesn't change anything
// in output (or logic). It is just a way for
// programmer to tell compiler about an
// optimization
void use(int* a, int* b, int* restrict c)
{
*a += *c;

// Since c is restrict, compiler will
// not reload value at address c in
// its assembly code. Therefore generated
// assembly code is optimized
*b += *c; 
}

int main(void)
{
int a = 50, b = 60, c = 70;
use(&a, &b, &c);
printf("%d %d %d", a, b, c);
return 0;
}
输出: 
120 130 70
 

最新文章

热点资讯

手游排行榜

CopyRight 2020-2030吾爱程序员

鄂ICP备2021004581号-8

本站资源收集于网络,如有侵权请联系我们:35492删除0109@qq.com