MSVC++ restrict keyword and local variables

假装没事ソ 提交于 2019-12-10 17:46:40

问题


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.

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);
}

Does the above example violate the rules of 'restrict'?


回答1:


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.




回答2:


C++ has no such keyword as restrict. Moreover in your example there are two different words: __restrict and RESTRICT. I think that the first word is implementation defined and the second word denotes a macro. It is C that has the keyword restrict.



来源:https://stackoverflow.com/questions/19391960/msvc-restrict-keyword-and-local-variables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!