“限制”是什么意思? [英] What does 'restrict' mean?

查看:83
本文介绍了“限制”是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在函数声明中看到了很多用法,比如


size_t fread(void * restrict ptr,size_t size,size_t nobj,FILE *

限制fp);


但关键字限制是什么意思?没有找到定义
$ K $ b& R 2nd。

解决方案

Niu Xiao< gn ***** *@gmail.com>写道:

我看到在函数声明中有很多用处,例如

size_t fread(void * restrict ptr,size_t size,size_t nobj,FILE *
限制fp);

但关键字限制是什么意思?没有定义
在K& R 2nd中找到。




restrict关键字


C99支持restrict关键字,允许涉及指针的某些

优化。例如:


void copy(int * restrict d,const int * restrict s,int n)

{

while (n - > 0)

* d ++ = * s ++;

}


C ++无法识别此关键字。


一个简单的解决方法,用于编译为/或
C或C ++的代码是使用一个宏作为restrict关键字:


#ifdef __cplusplus

#define restrict / * nothing * /

#endif


(这个功能很可能是许多

C ++编译器的扩展。如果是,它也可能被允许作为

参考修饰符。作为指针修饰符。)


[C99:?§6.2.5,6.4.1,6.7.3,6.7.3.1,7,A.1.2,J.2]

[C ++ 98:?§2.11]

a?¢

来自 href =http://david.tribble.com/text/cdiffs.htm#C99-restricttarget =_ blank> http://david.tribble.com/text/cdiffs.htm#C99-restrict


-

burton samograd kruhft .at。 gmail

kruhft.blogspot.com www.myspace.com/kruhft metashell.blogspot.com


Burton Samograd写道:

Niu Xiao< gn ****** @ gmail.com> ;写道:

我看到在函数声明中有很多用处,例如

size_t fread(void * restrict ptr,size_t size,size_t nobj,FILE *
限制fp);

但关键字限制是什么意思?没有定义
在K& R 2nd中找到。



restrict关键字

C99支持restrict关键字,允许某些
涉及指针的优化。例如:

void copy(int * restrict d,const int * restrict s,int n)
{
while(n - > 0)
* d ++ = * s ++;
}

C ++无法识别此关键字。

一个简单的解决方法,用于编译为
#ifdef __cplusplus
#define restrict / * nothing * /
#endif

(这个功能可能是许多C ++编译器提供的扩展。如果是,它也可能被允许作为
引用修饰符和指针修饰符。)

[C99:§6.2.5,6.4.1,6.7.3,6.7.3.1,7,A.1.2,J.2]
[C ++ 98:§2.11 ]



来自 http://david.tribble.com/text/cdiffs.htm#C99-restrict


但优化指针是什么?


Burton Samograd< kr ********** @ gmail.com>写道:

Niu Xiao< gn ****** @ gmail.com>写道:

我看到在函数声明中有很多用处,例如

size_t fread(void * restrict ptr,size_t size,size_t nobj,FILE *
限制fp);

但关键字限制是什么意思?在K& R 2nd中找不到定义。



限制关键字

C99支持restrict关键字,



请注意,你在K& R 2中找不到它的原因是那本书

涵盖C(几乎 - )89,而不是C99。 br />

Richard


I see a lot of use in function declarations, such as

size_t fread(void* restrict ptr, size_t size, size_t nobj, FILE*
restrict fp);

but what does the keyword ''restrict'' mean? there is no definition found
in K&R 2nd.

解决方案

Niu Xiao <gn******@gmail.com> writes:

I see a lot of use in function declarations, such as

size_t fread(void* restrict ptr, size_t size, size_t nobj, FILE*
restrict fp);

but what does the keyword ''restrict'' mean? there is no definition
found in K&R 2nd.



restrict keyword

C99 supports the restrict keyword, which allows for certain
optimizations involving pointers. For example:

void copy(int *restrict d, const int *restrict s, int n)
{
while (n-- > 0)
*d++ = *s++;
}

C++ does not recognize this keyword.

A simple work-around for code that is meant to be compiled as
either C or C++ is to use a macro for the restrict keyword:

#ifdef __cplusplus
#define restrict /* nothing */
#endif

(This feature is likely to be provided as an extension by many
C++ compilers. If it is, it is also likely to be allowed as a
reference modifier as well as a pointer modifier.)

[C99: ?§6.2.5, 6.4.1, 6.7.3, 6.7.3.1, 7, A.1.2, J.2]
[C++98: ?§2.11]

a?¢

from http://david.tribble.com/text/cdiffs.htm#C99-restrict

--
burton samograd kruhft .at. gmail
kruhft.blogspot.com www.myspace.com/kruhft metashell.blogspot.com


Burton Samograd wrote:

Niu Xiao <gn******@gmail.com> writes:

I see a lot of use in function declarations, such as

size_t fread(void* restrict ptr, size_t size, size_t nobj, FILE*
restrict fp);

but what does the keyword ''restrict'' mean? there is no definition
found in K&R 2nd.


restrict keyword

C99 supports the restrict keyword, which allows for certain
optimizations involving pointers. For example:

void copy(int *restrict d, const int *restrict s, int n)
{
while (n-- > 0)
*d++ = *s++;
}

C++ does not recognize this keyword.

A simple work-around for code that is meant to be compiled as
either C or C++ is to use a macro for the restrict keyword:

#ifdef __cplusplus
#define restrict /* nothing */
#endif

(This feature is likely to be provided as an extension by many
C++ compilers. If it is, it is also likely to be allowed as a
reference modifier as well as a pointer modifier.)

[C99: §6.2.5, 6.4.1, 6.7.3, 6.7.3.1, 7, A.1.2, J.2]
[C++98: §2.11]

?

from http://david.tribble.com/text/cdiffs.htm#C99-restrict



but what optimizations invoving pointers?


Burton Samograd <kr**********@gmail.com> wrote:

Niu Xiao <gn******@gmail.com> writes:

I see a lot of use in function declarations, such as

size_t fread(void* restrict ptr, size_t size, size_t nobj, FILE*
restrict fp);

but what does the keyword ''restrict'' mean? there is no definition
found in K&R 2nd.



restrict keyword

C99 supports the restrict keyword,



And note that the reason you won''t find it in K&R 2 is that that book
covers C(almost-)89, not C99.

Richard


这篇关于“限制”是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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