DonationCoder.com forum
 
Read-only archive. This is a static copy of the DonationCoder.com forum. Reading and searching work as before; logging in, posting and private messages do not.
Archive of 2026-10-21 · About this archive · Main site

Author Topic: what is __restrict keyword in c?  (Read 14,962 times)

megatron

  • Participant
  • Joined in 2008
  • *
  • Posts: 23
what is __restrict keyword in c?
« on: January 23, 2009, 06:55 PM »
what is __restrict keyword in c?

MilesAhead

  • Supporting Member
  • Joined in 2009
  • **
  • Posts: 7,736
    • Coding Snacks
Re: what is __restrict keyword in c?
« Reply #1 on: January 23, 2009, 07:36 PM »
Looks like it's an MS specific modifier.  I just read the entry on MS site for it here:
http://msdn.microsoft.com/en-us/library/5ft82fed(VS.80).aspx

and I'm still not sure what it does.  :)

Ehtyar

  • That News Guy
  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 1,237
    • Plugin Coding Medal
    • Gathering Badge
Re: what is __restrict keyword in c?
« Reply #2 on: January 23, 2009, 07:48 PM »
Basically, it informs the compiler that there are no other variables in the current function that point to the same data as your __restricted variable. It's used to manually prevent the compiler from having the CPU write data to memory, and then try to read it back again too soon, in which case the CPU will stall for a rather large number of cycles because there wasn't enough time to get the data all the way to main memory before the CPU tried to pull it back again. It is not an MS-specific extension.

I was surprised to find a rather detailed article demonstrating the use of __restricted here.

Hope this helps, Ehtyar.
« Last Edit: January 23, 2009, 07:55 PM by Ehtyar »

MilesAhead

  • Supporting Member
  • Joined in 2009
  • **
  • Posts: 7,736
    • Coding Snacks
Re: what is __restrict keyword in c?
« Reply #3 on: January 23, 2009, 11:09 PM »
I think you'll find when Microsoft adds its own wrinkle it precedes it with double underscores.  If you are using MS compilers it's always a good idea to check their documention.  A good example is try  exception handling in C++.  There's the C++ exception mechanism using try..catch, then there's MS home grown Structured Exception Handling that uses __try ..except instead of try.  They are not the same.

In this case it may just be using C 'restrict' keyword in C++ as __restrict but I wouldn't take anything for granted.

Here's a simple explanation more succinct than on MS page I think:
http://www.devx.com/tips/Tip/13825

(for some reason we all want to put the 'ed' on the end of restrict to get restricted.  I just did it too) :)
« Last Edit: January 23, 2009, 11:34 PM by MilesAhead »

megatron

  • Participant
  • Joined in 2008
  • *
  • Posts: 23
Re: what is __restrict keyword in c?
« Reply #4 on: January 25, 2009, 08:24 PM »
I was surprised to find a rather detailed article demonstrating the use of __restricted here.

Very informative link… I think I got what this keyword does…

Original address: https://www.donationcoder.com/forum/index.php?topic=16722.0