473,786 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

another very basic question :-)

Hello erveryone,I am a newcomer here and the word of c.
Here I have a question confused me a lot that when I read codes I
found some declaration like that:
"int regcomp(regex_t *restrict comoiled, const char *restrict
parttern, int cflags) "
or something like that
" int strnlen(const char FAR *s, int count)"
what does the "restrict" or "FAR" means?

--thanks advance
Sep 26 '08 #1
4 1392
sagi wrote:
Hello erveryone,I am a newcomer here and the word of c.
Here I have a question confused me a lot that when I read codes I
found some declaration like that:
"int regcomp(regex_t *restrict comoiled, const char *restrict
parttern, int cflags) "
or something like that
" int strnlen(const char FAR *s, int count)"
what does the "restrict" or "FAR" means?
FAR is not part of standard C. I believe that it is either a keyword or
a macro. Either way, it only works on certain systems (which don't
include any system I write programs for). You would get a better answer
than I could give you by asking in a newsgroup for the appropriate
compiler or operating system.

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(cons t 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]++;
}
}
}

What happens if this function is called as follows? :

char alphabet[] = "abcde";
blankchars(alph abet, alphabet);

On my home computer, the result of this call will be that alphabet now
contains the string "bddff". Do you understand why?

Well, that is probably not what anyone would want the function to do. In
fact, there's no reasonable thing for the function to do when the
blanklist and the string overlap in any way.

You might think that it should check for overlap between the blanklist
and the output string. The obvious way to do this would seem to be
compare blanklist, string, and strlen(blanklis t) and strlen(string).
Unfortunately, if blanklist and string do NOT overlap, then comparing
them with <, >, <=, >= or by subtracting them has behavior that is not
defined by the C standard. On many machines it will work as you might
expect, but on many real machines, it will produce erroneous results or
possibly even cause your program to abort().

You can compare pointers to different arrays for equality, and there's a
perfectly portable way to check for overlap that makes use of that fact.
Just compare a pointer to every character in one string for equality
with pointers to the starting and ending characters of the other string,
and vice versa. However, it's rather inefficient.

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.

However, just because you've documented that restriction, doesn't mean
that that compiler knows anything about it. Because you've decided not
to worry about overlapping arguments, you could have re-written the
innermost loop as follows:

int c = *blanklist;

for(size_t i; string[i]; i++)
{
if(string[i] == c)
string[i] = ' ';
}

When there is no overlap, this code produces exactly the same result as
the original program. This is a very simple optimization, and under
other circumstances, virtually any modern compiler would perform
optimizations like this one for you, even at the lowest optimization levels.

However, in this particular circumstance, the compiler cannot perform
this optimization for you. The compiler has to generate code which will
work as required by the C standard, even if string[i] and *blanklist
refer to the same character, and that's not the case for the optimized
version of the code. With the optimized version, blankchars(alph abet,
alphabet) would produce "fffff" (on my machine). The compiler thinks
it's important to make sure that this case produces "bddff", and will
therefore not perform the optimization. It doesn't know that you don't
care about that case.

This is where "restrict" comes in. If we change the function declaration to

void blankchars(cons t 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. It's no longer the case that we can
say that the result of calling blankchars(alph abet, alphabet) is to set
alphabet to "bddff". It is now technically possible that just about
anything could happen, including causing your program to abort.

In this particular case, it is most likely to produce "fffff", but in
more complicated cases, the problems created by the permitted
optimizations could be arbitrarily bad. However, to be fair, in those
more complicated cases, the problems would probably also be pretty bad
if the "restrict" keyword was not used.
Sep 26 '08 #2
sagi wrote:
Hello erveryone,I am a newcomer here and the word of c.
Here I have a question confused me a lot that when I read codes I
found some declaration like that:
"int regcomp(regex_t *restrict comoiled, const char *restrict
parttern, int cflags) "
or something like that
" int strnlen(const char FAR *s, int count)"
what does the "restrict" or "FAR" means?
FAR (and often many other ones you will see like NEAR DATA IDATA XDATA
RDATA etc. even FAR FAR are basically special compiler depedant things
which tell the compiler what sort of memory is being pointed to. This
is most common on embedded systems which can have internal RAM, external
RAM, a second external RAM chip at different addresses etc.

For example its typical for an embedded processor to have internal RAM
and external ram. So you might be able to declare variables like:

int foo[32];
int FAR foo[32];

one will take up space in internal ram and one in external ram, it all
depends on the compiler. The compiler will have documentation about
what it actually does. Typically pointers to external ram may actually
be of different size or need to page out memory etc so they also use
the FAR
Sep 26 '08 #3
REH
On Sep 26, 6:04*pm, MisterE <mist...@no.ema il.okwrote:
FAR (and often many other ones you will see like NEAR DATA IDATA XDATA
RDATA etc. even FAR FAR are basically special compiler depedant things
which tell the compiler what sort of memory is being pointed to. This
is most common on embedded systems which can have internal RAM, external
RAM, a second external RAM chip at different addresses etc.

For example its typical for an embedded processor to have internal RAM
and external ram. So you might be able to declare variables like:

int foo[32];
int FAR foo[32];

one will take up space in internal ram and one in external ram, it all
depends on the compiler. The compiler will have documentation about
what it actually does. Typically pointers to external ram may actually
be of different size or need to page out memory etc so they also use
the FAR
OT, but it is more likely he is using a compiler for a segmented
architecture, like DOS. FAR doesn't indicate any particular type of
RAM. It indicated a far pointer, which is one that contains both the
segment and offset of the address.

REH
Sep 27 '08 #4
On Fri, 26 Sep 2008 17:51:18 +0000, James Kuyper <ja*********@ve rizon.net>
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(cons t 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(cons t 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
Sep 27 '08 #5

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

Similar topics

3
2758
by: John | last post by:
Hello, I need to know which programs are opn on another machine in my network, these programs are opened from a shared drive on the server. How can I do this ? I've already dowloaded a sample program form vbnet.mvps.org but it doesn't work?? Greets John
7
3124
by: jesse | last post by:
In java, one constructor can call another constructor through this(...) for instance class foo { public: foo(int k) { this(k,false)}; foo(int k, boolean m){...}; }
3
2572
by: sparks | last post by:
I was copying fields from one table to another. IF the var name starts with milk I change it to egg and create it in the destination table. It works fine but I want to copy the description as well. Short version :) For Each fld In tdf.Fields pos = InStr(fld.Name, "milk") If pos > 0 Then
26
21341
by: Paul | last post by:
public class A { public A () { // here I would like to call the second version of _ctor, how to accomplish this ? } public A (int a, int b, int c) {
2
1554
by: moonriver | last post by:
In a xml file, can we make reference to another xml file so that all contents of the latter xml file will be included into the first xml file? Had better give me an example for details.
2
1646
by: Marcelo | last post by:
Hi guys, I'm using the following code to send values from one page to another, but seems to me, that the event Page_Load in the receiving page never fires. Why? This is the code: Thanks -*-*-*-*-*-*
14
2393
by: Craig Buchanan | last post by:
If I have two custom vb.net classes, where 80% of the properties are alike and there is one method with a matching signature, can i cast between one and the other? do i need to have each class implement a common interface? thanks, craig buchanan
0
1596
by: tonyrb | last post by:
Hi all, First time posting here. I was able to do this in Visual Basic 6, but in VB 2005 Express I am having a hard time with drag'n'drop. Version I am using is Visual Basic 2005 Express. How do I get two textboxes to swap text? All the examples I have found are only to either copy or move text from one textbox to another not swap the text.
11
2300
by: =?Utf-8?B?UGV0ZXI=?= | last post by:
I have seen the terms Visual Basic 2005 and VB.NET. It seems that sometimes they seem to be referring to the same thing but sometimes they are not. I also run into terms like VB9 and VB10.
2
1769
by: manontheedge | last post by:
I've got a program in C++, but I want to create a front end user interface for it using Visual Basic. I'm not looking for alternatives, I'm just curious, how would I do that? Just as a basic example, lets say I have a Visual Basic form, and there are two text boxes that take strings from the user. Then the user clicks a button. ...then, I want to send these two arguments to my C++ code. After the C++ code does some stuff, I want it to...
0
9647
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9492
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10360
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10163
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7510
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6744
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5532
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4064
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.