Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

restrict — restricting aliasing

restrict qualifies a pointer and imposes requirements on object access. Violating these requirements causes undefined behavior.

Version: C23; repository compiler flag, production runtime not attested

Syntax

T * restrict p;
void copy_values(int n, int * restrict dst, const int * restrict src);

restrict qualifies the pointer, not the pointed-to object. For a function parameter, its rules apply during each execution of the function body.

If an object accessed through that pointer is also modified, all accesses to it must use pointers based on that same restricted pointer. Derived pointers are allowed; sharing objects that are only read is also allowed.

This is a programmer obligation, not a runtime check. A violation means undefined behavior. restrict enables optimizations but does not guarantee a speedup.

Examples

#include <stdio.h>

static void copy_values(int n, int * restrict dst,
                        const int * restrict src)
{
    for (int i = 0; i < n; ++i) {
        dst[i] = src[i];
    }
}

int main(void)
{
    const int input[3] = {2, 4, 6};
    int output[3] = {0};
    copy_values(3, output, input);
    printf("%d %d %d\n", output[0], output[1], output[2]);
    return 0;
}

Result

2 4 6

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources