CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    __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 offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    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. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. 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/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

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

    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 offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    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. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. 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/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured