C99中新增了类型修饰符(qualifier) restrict.docVIP

  • 1
  • 0
  • 约5.84千字
  • 约 4页
  • 2018-01-02 发布于河南
  • 举报

C99中新增了类型修饰符(qualifier) restrict.doc

C99中新增了类型修饰符(qualifier) restrict

学习札记 在C99中新增了类型修饰符(qualifier) restrict ========================8==================================== restrict这种修饰符只适用于指针. 由restrict修饰的指针是最初唯一对指针所指向的对象进行存取的办法, 仅当第二个指针基于第一个时,才能对对象进行存取. 因此,对对象的存取都限定于基于有restrict修饰的指针表达式中. 由restrict修饰的指针主要被用做函数指针,或者指向由malloc()分配的内存变量. restrict数据类型不改变程序的语义. =======================8======================================= ? 感觉说的有的乱,可能是我的理解能力不够吧。然后在CSDN看到了这个: ? ========================8==================================== restrict是C99版新增加的关键字!???如下:? ? ?? C99 ? 中新增加了 ? restrict ? 修饰的指针:?由 ? restrict ? 修饰的指针是最初唯一对指针所指向的对象进行存取的方法,仅当第二个指针基于第一个时,才能对对象进行存取。对对象的存取都限定于基于由 ? restrict ? 修饰的指针表达式中。 ? ?? 由 ? restrict ? 修饰的指针主要用于函数形参,或指向由?? malloc() ??分配的内存空间。restrict ??数据类型不改变程序的语义。 ?编译器能通过作出?? restrict ??修饰的指针是存取对象的唯一方法的假设,更好地优化某些类型的例程。? ? ?? [典型例子] ? memcpy() ? 在 ? C99 ? 中,restrict ? 可明确用于 ? memcpy() ? 的原型,而在 ? C89 ? 中必须进行解释。?? void ? *memcpy (void ? *restrict ? str1, ? const ? void ? *restrict ? str2, ? size_t ? size); ? /* ??通过使用 ? restrict ? 修饰 ? str1 ? 和 ? str2 ? 来保证它们指向不重叠的对象 ? */ ? =======================8======================================= ? 很多人说这个关键字主要是用来加强编译器优化的,理由也很简单:“由restrict修饰的指针是最初唯一对指针所指向的对象进行存取的办法,仅当第二个指针基于第一个时,才能对对象进行存取.”这样下面的代码就可以被很好的优化, void fcpy(float *restrict a, float *restrict b, ? ???float *restrict aa, float *restrict bb, int n) { ?int i; ?for(i = 0; i n; i++) { ??? aa[i]=a[i]; ??? bb[i]=b[i]; ?} } 这意味着这些拷贝循环能够“并行”(in parallel),由于 例如 aa != b ? 但还是喜欢下面的例子,从它可以看出restrict不仅仅可以被用来加强编译器的优化,还是解决我们代码中存在的隐患。 ? =====================8================================ Restrict Pointers One of the new features in the recently approved C standard C99, is the restrict pointer qualifier. This qualifier can be applied to a data pointer to indicate that, during the scope of that pointer declaration, all data accessed through it will be accessed only through that pointer but not through any other pointer. The restrict keyword thus enables the compiler to perform certain optimizations based on the premise that a given object cannot be c

文档评论(0)

1亿VIP精品文档

相关文档