__restrict (keyword)
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: __restrict (keyword)

  1. #1
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,440

    __restrict (keyword)

    I've just been sent some code which uses the __restrict keyword. I looked it up at MSDN which gives the following example:

    Code:
    // __restrict_keyword.c
    // compile with: /LD
    // In the following function, declare a and b as disjoint arrays
    // but do not have same assurance for c and d.
    void sum2(int n, int * __restrict a, int * __restrict b, 
              int * c, int * d) {
       int i;
       for (i = 0; i < n; i++) {
          a[i] = b[i] + c[i];
          c[i] = b[i] + d[i];
        }
    }
    Maybe it's just me, but that example doesn't really tell me what __restrict is doing. What exactly does it allow (or disallow) ?
    "A problem well stated is a problem half solved. - Charles F. Kettering

  2. #2
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,214

    Re: __restrict (keyword)

    __restrict is basically a promise to the compiler that for the scope of the pointer, the target of the pointer will only be accessed through that pointer (and pointers copied from it).

    See http://stackoverflow.com/questions/7...word-mean-in-c

    This gives various references to papers etc with explanations and examples. Have fun!
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C, C++ Compiler: Microsoft VS2017

  3. #3
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,440

    Re: __restrict (keyword)

    Thanks 2kaud, it makes a bit more sense now!

    BTW, the original code used __restrict__ (with extra underscores at the end). I'm guessing that's essentially the same thing but for a different compiler (gcc?)
    "A problem well stated is a problem half solved. - Charles F. Kettering

  4. #4
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,214

    Re: __restrict (keyword)

    Yeah - restrict is not a part of c++ yet (its from c) so its implementation is compiler specific - hence the beginning __. It might even do nothing!
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C, C++ Compiler: Microsoft VS2017

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This a Codeguru.com survey!


HTML5 Development Center