473,396 Members | 1,995 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

restrict keyword questions

Restrict keyword questions

How far does the guarantee that an object is not accessed through another
pointer go? I mean, all examples I have seen are simple stuff like:

int f (int *restrict x, int *restrict y)
{
*x = 0;
*y = 1;
return *x;
}

Ok, the "return *x" can be optimised to "return 0" in this case, but what
happens if we add a function call:

int f (int *restrict x, int *restrict y)
{
*x = 0;
my_black_box_of_pain();
*y = 1;
return *x;
}

Does the guarantee that the x object is not modified persist? I mean
my_black_box_of_pain() could easily modify the memory area x points to. Now,
if the guarantee persists, then one should probably never use the restrict
keyword except when optimising very simple self-contained functions (i.e. no
calls to other functions). Because trying to ensure that all functions
called by a function which uses restrict parameters, and the functions
called by those functions etc. do not violate the contract is a recipe for
disaster. It so easy to introduce a bug there it is almost silly.
If the guarantee does NOT persist the restrict keyword is almost useless,
because you rarely see a C function which doesn't call other functions.
One way or the other, the restrict keyword seems to be for special interest
groups only. Right or not?

BTW, does the restrict keyword actually guarantee "mem area not modified
through another pointer" or not.
I wonder because I have heard different claims. I searched this group and
people here seem to think that restrict actually makes guarantees about the
object the restricted pointer points to (i.e. the mem area). However, this
article:

http://www.cellperformance.com/mike_...rict_keyw.html

Seems to contradict these claims. Quotes
---------
"
The restrict keyword does not declare that the object being pointed to is
completely without aliases, only that the addresses that are loaded and
stored from are unaliased.
"

"
Memory windows can overlap and still be non-aliased.
"
------------

Now think of something like

int foo(int * restrict a, int * restrict b) {
a[1] = 20;
*b = 10;
return a[1];
}

foo(x, &x[1]);

a and b are not aliased here because they point to different addresses i.e.
the restrict requirements are met, at least according to that article. The
memory windows overlap but that is supposed to be ok..

I am definitely confused now..

Mar 29 '08 #1
0 2955

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
by: gc | last post by:
Hi, What is the purpose of the restrict keyword? gc
7
by: tweak | last post by:
Can someone give me a short example as how to best use this keyword in your code? This is my understanding: by definition restrict sounds like it is suppose to restrict access to memory...
11
by: pemo | last post by:
If you were to compile/run the code below, and get the result '30', I'd be very interested to know what compiler you're using - and its optimisation settings #include <stdio.h> int test(int...
12
by: Me | last post by:
I'm trying to wrap my head around the wording but from what I think the standard says: 1. it's impossible to swap a restrict pointer with another pointer, i.e. int a = 1, b = 2; int *...
21
by: Niu Xiao | last post by:
I see a lot of use in function declarations, such as size_t fread(void* restrict ptr, size_t size, size_t nobj, FILE* restrict fp); but what does the keyword 'restrict' mean? there is no...
2
by: Frederick Gotham | last post by:
I did a search through the C++ Standard there, and couldn't find any mention of "restrict" or "__restrict__". Firstly, could someone please confirm whether "restrict" is mentioned in the C++...
23
by: raashid bhatt | last post by:
what is restrict keyword used for? eg int *restrict p;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.