0
0
CConceptBeginner · 3 min read

What is restrict keyword in C: Explanation and Example

The restrict keyword in C is a pointer qualifier that tells the compiler the pointer is the only way to access the object it points to during its lifetime. This helps the compiler optimize code by assuming no other pointer will modify the same data.
⚙️

How It Works

Imagine you have a box that only one person can open and change its contents. The restrict keyword tells the compiler that a pointer is like that box's single key. No other pointer will access or change the data it points to while this pointer is in use.

This allows the compiler to make faster code because it doesn't have to worry about other pointers changing the data unexpectedly. It can safely reorder or combine operations knowing the data won't be changed elsewhere.

Without restrict, the compiler must assume multiple pointers might access the same memory, so it must be more careful and generate slower code.

💻

Example

This example shows two functions: one uses restrict pointers and the other does not. The restrict version tells the compiler the pointers do not overlap, allowing better optimization.

c
#include <stdio.h>

void add_arrays(int *restrict a, int *restrict b, int *restrict c, int n) {
    for (int i = 0; i < n; i++) {
        c[i] = a[i] + b[i];
    }
}

void add_arrays_no_restrict(int *a, int *b, int *c, int n) {
    for (int i = 0; i < n; i++) {
        c[i] = a[i] + b[i];
    }
}

int main() {
    int x[] = {1, 2, 3};
    int y[] = {4, 5, 6};
    int z[3];

    add_arrays(x, y, z, 3);

    for (int i = 0; i < 3; i++) {
        printf("%d ", z[i]);
    }
    printf("\n");

    add_arrays_no_restrict(x, y, z, 3);

    for (int i = 0; i < 3; i++) {
        printf("%d ", z[i]);
    }
    printf("\n");

    return 0;
}
Output
5 7 9 5 7 9
🎯

When to Use

Use restrict when you know a pointer is the only way to access a particular memory area during a function's execution. This is common in performance-critical code like graphics, scientific computing, or data processing.

It helps the compiler generate faster code by avoiding unnecessary memory access checks. However, misuse can cause bugs if other pointers do access the same memory, so use it only when you are sure about pointer exclusivity.

Key Points

  • restrict is a promise that only this pointer accesses the object.
  • It enables better compiler optimizations.
  • Misuse can cause undefined behavior if the promise is broken.
  • It applies only to pointers, not to other variable types.
  • Introduced in C99 standard.

Key Takeaways

The restrict keyword tells the compiler a pointer is the only way to access its data, enabling optimizations.
Use restrict only when you are sure no other pointer accesses the same memory during its lifetime.
It helps improve performance in functions that manipulate arrays or buffers.
Misusing restrict can lead to unpredictable program behavior.
restrict was introduced in the C99 standard and applies only to pointers.