Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

restrict keyword only to write buffers?

Does restrict keyword makes sense only to the pointers that we write to?

I often see the usage of restrict for both read and write buffers. for example in memcpy function,

void* memcpy( void *restrict dest, const void *restrict src, size_t count );.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

If we remove restrict to src, do we change the functionality of this function?

In general, if we have a function
void fun(float * a, float *b .....); when only a is modified, do we need restrict for other pointers too?

>Solution :

Does restrict keyword makes sense only to the pointers that we write to?

No. restrict keyword also makes sense to pointers that we read from.

If we remove restrict to src, do we change the functionality of this function?

restrict has no effect on functionality. restrict doesn’t change the "the tasks that software program is able to do".

Additionally, restrict did not exist before C99, yet memcpy had the same functionality.

if we have a function void fun(float * a, float *b …..); when only a is modified, do we need restrict for other pointers too?

You never "need" restrict. It’s an addition, that you can add in specific situations to achieve better performance, given that the requirements of restrict are held. The requirement of "not modifying other pointers" is not enough to validate using restrict.

In the case of memcpy, dest and src are not allowed to overlap.

Related: there are situations, where compiler can infer non-aliasing of pointers given only some pointers are restrict. Such a case is discussed in https://en.cppreference.com/w/c/language/restrict in section "Function parameter", but the link is overall related and recommended to read.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading