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. 4 2189
"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
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
"Arthur J. O'Dwyer" <aj*@nospam.and rew.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. \_/
"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. \_/ This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: gc |
last post by:
Hi,
What is the purpose of the restrict keyword?
gc
|
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 location(s) pointed to, so that
only one declared pointer can store that address and access the data in
those memory blocks, where I the data in those location(s) can be changed.
Is that a correct understanding?
|
by: ccdrbrg |
last post by:
I'm having trouble understanding restrict.
Can someone provide a layman's explanation.
Chad
|
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);
But this seems a mite dangerous to me ... a restricted pointer ... is
*assumed* to be the only to access an object.
So, mightn't using such a prototype subtly imply that the compiler
will *actively check* that s1 and s2 do not point to the same
|
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 * restrict ap = &a;
int * restrict bp = &b;
| |
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 definition found
in K&R 2nd.
|
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 objects. It will be
good, if explained with example.
Appriciate your help in this regard.
Thanks,
|
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, int *restrict y)
{
*x = 0;
*y = 1;
return *x;
|
by: raashid bhatt |
last post by:
what is restrict keyword used for?
eg int *restrict p;
|
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...
|
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,...
| |
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...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |