473,503 Members | 1,655 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

`restrict' qualifer

Greetings,

Are the following inferences of mine correct?

1. #include <string.h>
char *strcpy(char * restrict s1,
const char * restrict s2);

a. s1 != s2
b. That means,

char s[10] = "vijoeyz";
(void) strcpy ( s, s ); /* though useless */

is not allowed!

2. #include <string.h>
size_t strlen(const char *s);

a. If there is only one parameter of type "pointer to T", for any type
T, then the `restrict' qualifier need not be used.

--
Women and cats do as they please. Men and dogs
should sit back, and learn to relax.
Nov 14 '05 #1
4 2170
"Vijay Kumar R Zanvar" <vi***********@globaledgesoft.com> wrote:
Are the following inferences of mine correct?

1. #include <string.h>
char *strcpy(char * restrict s1,
const char * restrict s2);

a. s1 != s2
Yes.
b. That means,

char s[10] = "vijoeyz";
(void) strcpy ( s, s ); /* though useless */

is not allowed!
Yes. Note that the definition following this declaration in the Standard
makes this explicit, even in C89:

# If copying takes place between objects that overlap, the behavior
# is undefined.
2. #include <string.h>
size_t strlen(const char *s);

a. If there is only one parameter of type "pointer to T", for any type
T, then the `restrict' qualifier need not be used.


Yes.

Richard
Nov 14 '05 #2

On Tue, 27 Apr 2004, Vijay Kumar R Zanvar wrote:

Are the following inferences of mine correct?

1. #include <string.h>
char *strcpy(char * restrict s1,
const char * restrict s2);

a. s1 != s2
Not necessarily. Yes, if we're to assume that when you
wrote 'strcpy' above you really meant 'strcpy', the function
that copies the target of 's2' into the target of 's1'. But
generally, no, we could have the pathological case

int ptreq(const char * restrict s1, const char * restrict s2)
{
return (s1 == s2);
}

that would be perfectly conforming and unable to generate
undefined behavior, no matter the values of 's1' and 's2'.
b. That means,

char s[10] = "vijoeyz";
(void) strcpy ( s, s ); /* though useless */

is not allowed!
Naturally. In the case of 'strcpy', the Standard explicitly
rules this sort of thing out (see N869 7.21.2.3 #2). But
again, in the general case, you *could* pass 's' twice to the
'ptrcmp' function I defined above, because that function never
tries to access the targets of those pointers --- only their
values.
2. #include <string.h>
size_t strlen(const char *s);

a. If there is only one parameter of type "pointer to T", for any type
T, then the `restrict' qualifier need not be used.


No. 'restrict' guarantees that the pointer will have a sort of
a "lock" on its target for its entire lifetime. This means that
the following program exhibits undefined behavior:

char g[] = "hello world";
void foo(const char * restrict s) {
g[0] = 'J';
puts(s);
}
int main(void) {
foo(g);
}

because the target of 's' is modified through 'g' during the
lifetime of 's'. I'm almost positive that by writing

char ga[] = "hello world";
char * restrict g = ga;

at file scope, you would be "preventing" anyone's tampering with
that string except through 'g', for the entire run of the program.
Quotes around "preventing" because it's not something the compiler
can catch; it just means anyone who tampers with that array invokes
undefined behavior on himself.
N.B.: You can't define

char restrict ga[] = "hello world";

by itself; 'restrict' can only be applied to pointers, according to
N869. I don't see why it can't apply to array objects too; perhaps
someone will enlighten me.

-Arthur
Nov 14 '05 #3
"Arthur J. O'Dwyer" <aj*@nospam.andrew.cmu.edu> writes:
N.B.: You can't define

char restrict ga[] = "hello world";

by itself; 'restrict' can only be applied to pointers, according to
N869. I don't see why it can't apply to array objects too; perhaps
someone will enlighten me.


Two arrays can never alias, so there's no need to restrict qualify them,
just like there is no need to const qualify them, as they can never be
modified anyway. If you like, you can think of arrays as implictly
const and restrict qualified.

(In case it's not clear: By "array", I mean the array object itself, not
its elements.)

An array and a pointer can alias, but that situation can be expressed by
(absence of) a restrict qualifier on the pointer.

Martin
--
,--. Martin Dickopp, Dresden, Germany ,= ,-_-. =.
/ ,- ) http://www.zero-based.org/ ((_/)o o(\_))
\ `-' `-'(. .)`-'
`-. Debian, a variant of the GNU operating system. \_/
Nov 14 '05 #4
"Vijay Kumar R Zanvar" <vi***********@globaledgesoft.com> writes:
Are the following inferences of mine correct?
Yes.
1. #include <string.h>
char *strcpy(char * restrict s1,
const char * restrict s2);

a. s1 != s2
b. That means,

char s[10] = "vijoeyz";
(void) strcpy ( s, s ); /* though useless */

is not allowed!


Not only that, but the arguments may not even overlap.

strcpy (s, s + 1);

would likewise invoke undefined behavior.

Martin
--
,--. Martin Dickopp, Dresden, Germany ,= ,-_-. =.
/ ,- ) http://www.zero-based.org/ ((_/)o o(\_))
\ `-' `-'(. .)`-'
`-. Debian, a variant of the GNU operating system. \_/
Nov 14 '05 #5

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

Similar topics

28
6383
by: gc | last post by:
Hi, What is the purpose of the restrict keyword? gc
7
2658
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...
1
2491
by: ccdrbrg | last post by:
I'm having trouble understanding restrict. Can someone provide a layman's explanation. Chad
2
2394
by: pemo | last post by:
In Harbison and Steele's book, they say that using 'restrict' allows functions like memcpy() to be prototyped like this: void * memcpy(void * restrict s1, const void * restrict s2, size_t n); ...
12
2470
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
6469
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
3241
by: venkat | last post by:
Hi, i came across restrict qualifier while looking the code. I haven't able to understand what does this do?. Can some one help me how does this makes the things restrict to an specified...
0
2964
by: copx | last post by:
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,...
23
4798
by: raashid bhatt | last post by:
what is restrict keyword used for? eg int *restrict p;
0
7199
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
7273
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,...
1
6982
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...
1
5000
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...
0
4667
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...
0
3161
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3150
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1501
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 ...
1
731
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.