Understand the `restrict` keyword in C99
Introduction
What problem does restrict solve?
Another example with SVE2
When can you use restrict
Next Steps
Understand the `restrict` keyword in C99
Who is this for?
This is an introductory topic for C developers who are interested in software optimization
What will you learn?
Upon completion of this Learning Path, you will be able to:
- Learn the importance of using the `restrict` keyword in C correctly
Prerequisites
Before starting, you will need the following:
- An Arm computer running Linux OS and a recent version of compiler (Clang or GCC) installed
Summary
This summary was drafted with an approved AI-assisted workflow and reviewed by Arm contributors before publication. Human technical review remains part of the process so the final page reflects engineering rigor, accuracy, and Arm editorial standards.
You’ll use the C99
restrict qualifier to describe non-overlapping pointer regions and enable compiler vectorization on Arm. First, you’ll examine how aliasing inhibits optimization. Then, you’ll add restrict where its contract is safe and inspect the generated assembly. The examples highlight SVE2 code for a byte-processing loop on Armv9-A. You’ll use the examples to recognize when restrict is valid or unsafe.Frequently asked questions
These FAQs were drafted with an approved AI-assisted workflow and reviewed by Arm contributors before publication. Human technical review remains part of the process so the final page reflects engineering rigor, accuracy, and Arm editorial standards.
Use
restrict when the memory regions referenced by the pointer parameters don’t overlap and the function has no other access path to those regions. If any argument can alias another, don’t use restrict.The compiler can assume no aliasing and might generate vectorized code. On Arm with SVE2, look for vector registers (
z0–z31) and predicated operations in the assembly.If one pointer argument derives from another, such as a pointer into the same array, the regions overlap. Passing those pointers to a
restrict-qualified function violates its contract.Inspect the generated assembly for SVE2 instructions and vector registers.
whilelo predicates and ld1b or st1b instructions with z registers indicate SVE2 code generation.Avoid
restrict if the function can reach the same memory through another alias, such as a global pointer or indirect access. The non-aliasing guarantee must hold throughout the function.