MSVC++ 限制关键字和局部变量 [英] MSVC++ restrict keyword and local variables
问题描述
我已经阅读了许多关于 restrict
关键字的帖子.但几乎我能找到的每个例子似乎都只涉及一个函数的输入参数,也许是一个单一的值.我需要澄清一下我的理解.
I've read a number of posts on the restrict
keyword. But virtually every example I can find seem to refer to input parameters only to a function and, perhaps a single value. I need to clarify my understanding.
我发现了一个函数,它的输入参数和局部变量看起来都完全违反了关键字规则.
I've found a function that looks like it totally violates the rules of the keyword with both an input parameter and a local variable.
使用指向缓冲区的 void*
调用此函数,并将指针声明为 __restrict
(这是 Microsoft Visual C++).然而在函数的后面,声明了一个 UCHAR*
类型的局部变量指针,并使其指向相同的受限输入参数缓冲区.
This function is called with a void*
to a buffer and the pointer is declared as __restrict
(this is Microsoft Visual C++). Yet later in the function, a local variable pointer of type UCHAR*
is declared and made to point to that same restricted input parameter buffer.
这是我正在谈论的功能的一个严重削减版本:
Here is a seriously chopped down version of the function I'm talking about:
void Foo(int nVersion, int nX, int nY, int nWidth, void * __restrict pBuffer) const
{
// ... blah blah blah
UCHAR * __restrict pBufferPtr = ((UCHAR *) pBuffer) + 10; // Isn't this aliasing?
const void * __restrict pData = (blah blah blah); //... Get from some function call;
memcpy(pBufferPtr, pData, nWidth);
}
上面的例子是否违反了restrict
的规则?
Does the above example violate the rules of restrict
?
推荐答案
restrict
关键字仅表示指针应指向内存的唯一部分.在上面的代码中,pBuffer
指向的东西,我们称之为A
,pBufferPtr指向A+10
,PData指向的是完全不同的东西,B
,所以没有违规.
The restrict
keyword only means that the pointers should point to unique portions of memory. In the above code, pBuffer
points to something, let's call it A
, pBufferPtr points to A+10
, PData points to something completely different, B
, so there's no violations.
这篇关于MSVC++ 限制关键字和局部变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!