Re: another very basic question :-)
- From: Anand Hariharan <znvygb.nanaq.unevunena@xxxxxxxxx>
- Date: Fri, 26 Sep 2008 22:45:12 -0500
On Fri, 26 Sep 2008 17:51:18 +0000, James Kuyper <jameskuyper@xxxxxxxxxxx>
wrote:
(...)
To understand the restrict keyword, consider the following function. It(...)
takes as arguments a list of characters, and a string, and it adds 1 to
any character in the string which matches one of the characters in the
list.
void blankchars(const char blanklist[], char string[]) {
if(blanklist == NULL || string == NULL)
return;
for( ; *blanklist; blanklist++)
{
for(size_t i=0; string[i]; i++)
{
if(string[i] == *blanklist)
string[i]++;
}
}
}
The simplest approach is simply to document that the function is not(...)
guaranteed to work properly if there's any overlap between the strings
pointed at by its two arguments. This is a very popular choice, and it's
the one take by the C standard library itself in most cases like that.
This is where "restrict" comes in. If we change the function declaration
to
void blankchars(const char restrict blanklist[], char restrict string[])
then this tells the compiler that developers and users of blankchars()
will work together to make sure that string[i] and *blanklist never
refer to the same object. That gives the compiler permission to perform
optimizations like the one above.
The down side of 'restrict' is that, if the requirements it imposes are
violated, the behavior is undefined.
That was an excellent description of 'restrict', James.
To the OP -
'restrict' is a new feature introduced to C primarily to enable the
compiler to perform optimisations.
You probably need to understand what it means, since if you use a
function whose prototype has one or more parameters qualified with
'restrict', you need to respect it by passing the correct arguments.
However, I suggest you to not write code that involves 'restrict' unless
you know what you are doing.
- Anand
.
- References:
- another very basic question :-)
- From: sagi
- Re: another very basic question :-)
- From: James Kuyper
- another very basic question :-)
- Prev by Date: Re: compiler could not find include *.h file
- Next by Date: Problem With C?
- Previous by thread: Re: another very basic question :-)
- Next by thread: Re: another very basic question :-)
- Index(es):
Relevant Pages
|