Kaz Kylheku
2023-05-24 23:41:30 UTC
I've noticed that both arguments of fopen are restrict-qualified.
What does this achieve?
If I have:
const char *path = "foobar";
const char *mode = path + 6;
FILE *f = fopen(path, mode);
is the behavior undefined, since the mode pointer accesses
something which is also accessed through path?
What if the code is this:
FILE *f = fopen("foobar", "r");
and the compiler itself merges the two string literals so
that they share storage?
The arguments are treated as immutable by fopen, so what is
the purpose of restrict?
The benefit of restrict is that when an object is being modified through
a restrict-qualified pointer, the implementation is not required to care
whether the object is aliased through another object. Access through the
other pointer can be cached as if that modification didn't happen.
If aliasing is occurring, the behavior is undefined, allowing the
implementor not to care about that situation.
But the arguments to fopen must not be modified.
How can fopen be made faster based on the assurance that the inputs do
not overlap, (and is that really worth breaking programs?)
What does this achieve?
If I have:
const char *path = "foobar";
const char *mode = path + 6;
FILE *f = fopen(path, mode);
is the behavior undefined, since the mode pointer accesses
something which is also accessed through path?
What if the code is this:
FILE *f = fopen("foobar", "r");
and the compiler itself merges the two string literals so
that they share storage?
The arguments are treated as immutable by fopen, so what is
the purpose of restrict?
The benefit of restrict is that when an object is being modified through
a restrict-qualified pointer, the implementation is not required to care
whether the object is aliased through another object. Access through the
other pointer can be cached as if that modification didn't happen.
If aliasing is occurring, the behavior is undefined, allowing the
implementor not to care about that situation.
But the arguments to fopen must not be modified.
How can fopen be made faster based on the assurance that the inputs do
not overlap, (and is that really worth breaking programs?)
--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @***@mstdn.ca
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @***@mstdn.ca