Re: another very basic question :-)



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
.



Relevant Pages

  • Re: A note on personal corruption as a result of using C
    ... impossible to write effective string validation routines by definition ... The C standard provides its own definition of the term "string", ... Array of characters with a terminator; this has no limit on the ... compiler can assume that two identifiers refer to the same object. ...
    (comp.programming)
  • Re: converting a string to a list
    ... format of the input data, ... languages is that you can treat the compiler as a black box, ... I input a string, the ... string is a sequence of characters. ...
    (comp.lang.lisp)
  • Re: A note on personal corruption as a result of using C
    ... "A string cannot contain Nuls" Yes it can. ... that can include embedded null characters. ... compiler can assume that two identifiers refer to the same object. ... that a C for loop isn't defined the way you want it to be. ...
    (comp.programming)
  • Re: Data Records from Flat File (COBOL Style)
    ... The COBOL PIC clause absolutely does not have an internal length. ... characters to another place in memory and adds a length (in this case, ... Picking the fields out of a larger string ... The compiler may or may not generate instructions to make ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Prothon should not borrow Python strings!
    ... """It does not make sense to have a string without knowing what encoding ... same cul de sac as Python. ... Prothon_String_As_ASCII // raises error if there are high characters ... Python's split between byte strings and Unicode strings is ...
    (comp.lang.python)