What does the
restrict keyword mean in C?
Take this example:
char* realpath(const char *restrict, char *restrict);
realpath(path, realpath) canonicalizes the filepath
path and puts the result in the buffer
realpath == NULL, it allocates a new string and returns a pointer to it.
restrict is a “type qualifier”. (Other things in this category are
volatile). This means, for some type
T, we can write
T restrict to get another type. For example,
char const * restrict is a type.
Actually, it only applies to pointer types, i.e.
T * restrict. So
int restrict is invalid, but
int * restrict is valid.
In a function, a parameter
T * restrict p means that the allocated object pointed at by
p is only pointed at by
p. That is, during the execution of the function body, the only way to access
*p is via
p itself (also allowing for pointer manipulation like
p++). Other variables in scope, such as other function parameters, or global variables, do not point at
*p; nor does the memory graph available from those variables contain any pointers to
The compiler can then make some optimizations.
More by Jim
- Your syntax highlighter is wrong
- Granddad died today
- The Three Ts of Time, Thought and Typing: measuring cost on the web
- I hate telephones
- The sorry state of OpenSSL usability
- The dots do matter: how to scam a Gmail user
- My parents are Flat-Earthers
- How Hacker News stays interesting
- Project C-43: the lost origins of asymmetric crypto
- The hacker hype cycle
- The inception bar: a new phishing method
- Time is running out to catch COVID-19
- A probabilistic pub quiz for nerds
- Smear phishing: a new Android vulnerability
Tagged . All content copyright James Fisher 2016. This post is not associated with my employer. Found an error? Edit this page.