何时使用限制和何时不

何时使用限制和何时不

本文介绍了何时使用限制和何时不的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的一般理解限制但我希望澄清一些细微之处。我有一个函数从一个缓冲区中读取一个null结尾的字符串,并写出一个URL连接codeD的版本在另一个缓冲区。该函数有这个签名(目前还没有限制

I have a general understanding of restrict but I'm hoping to clarify some fine points. I have a function that reads a null-terminated string from one buffer and writes out a URL encoded version in another buffer. The function has this signature (currently without restrict):

char const *StringUrlEncode(char const *unencoded,
                            char *encoded,
                            char *encodedEnd);

unen codeD 是我的空值终止的源字符串。目标缓冲区重新由连接codeD 连接codedEnd ,其中psented $ P $ 连接codeD 指向第一个字符在缓冲区中,连接codedEnd 指向第一个字符的之后的缓冲区,即功能会写字符 s到,但的不包括的位置由连接codedEnd 指向 - 这是你的基本开始 / 结束迭代器对,如果你熟悉C ++ STL约定。

unencoded is my null-terminated source string. The destination buffer is represented by encoded and encodedEnd, where encoded points to the first char in the buffer and encodedEnd points to the first char after the buffer, i.e. the function will write chars up to but not including the location pointed to by encodedEnd -- this is your basic begin/end iterator pair if you're familiar with C++ STL conventions.

如果我想补充限制这个功能,它应该只适用于前两个参数:

If I add restrict to this function, should it only be applied to the first two parameters:

char const *StringUrlEncode(char const *restrict unencoded,
                            char *restrict encoded,
                            char *encodedEnd);

或者是有一些好处,我没有将它添加到所有三个参数理解?

or is there some benefit I'm not understanding by adding it to all three parameters?

我可以看到,使得输入和输出缓冲器限制帮助编译器知道,他们不重叠。但由于最后一个参数,连接codedEnd ,只用来标记输出缓冲区的末尾,我在想,限制不会真的是这里的编译器的任何帮助(虽然我认为它不会伤害比增加不必要的噪声函数声明除外)。

I can see that making the input and output buffers restrict helps the compiler know that they don't overlap. But since the last parameter, encodedEnd, is only used to mark the end of the output buffer, I'm thinking that restrict wouldn't really be any help to the compiler here (though I assume it wouldn't hurt, other than adding unnecessary noise to the function declaration).

推荐答案

尝试迈克阿克顿的文章here.限制是因为这两个不使用它的性能影响,并错误地使用它的后果可怕。

Try Mike Acton's article here. Restrict is frightening because of both the performance implications of not using it and the consequences of using it incorrectly.

在你的情况,这听起来像你可以安全地应用于限制所有三个三分球为无别名相同的内存区域。但是,有将是几乎没有任何业绩受益于使用它的第三个指针。

In your case, it sounds like you could safely apply restrict to all three pointers as none alias the same memory area. However there is going to be little to no performance benefit from using it on the third pointer.

这篇关于何时使用限制和何时不的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 10:41