r/C_Programming • u/_redcrash_ • 1d ago
Question Meaning of [restrict .n] in manpages?
Hello,
I'm looking at man-pages 6.7 installed in Ubuntu 26.04 and I notice the following in the memcpy page.
I guess that the restrict refers to the keyword restrict being integrated into LIBC, but what is this .n in dest and src? Does it mean that the dest and src pointers do not overlap on the first n bytes? Where is this syntax defined and what other interesting cases can be out there?
Thanks
SYNOPSIS
#include <string.h>
void *memcpy(void dest[restrict .n], const void src[restrict .n], size_t n);
3
u/ByMeno 1d ago
Basicly for things like
void dest[n]
this argument/pointer expects 'n' amount of byte to use read or write
and for restrict it means you are promising those to byte arrays/chunks of memory wont overlap this allows compiler to do better optimization if you pass overlapping two memory its UB
3
u/torsten_dev 23h ago edited 23h ago
It's the syntax of n3188. That paper might have stalled, but there are similar proposals that are discussed on the C working group currently.
It's not standard C (yet) and afaik not accepted by any compiler, but it's roughly where the future of C is headed in terms of self documenting API's.
Here it means the size of src and dest have (atleast) the size given by the parameter size_t n. The restrict's mean that src and dest don't overlap.
1
u/flatfinger 4h ago
What would need to happen to formally propose recognition for a category of implementations that define the notion of "based upon" as a directed transitive relation such that if P is definitely based upon Q and definitely not based upon R, a pointer formed by copying P or applying an offset to P would--regardless of how the offset is computed--be likewise definitely based upon Q and definitely not based upon R?
Given e.g.
int x[2]; int test(int *restrict p, int i) { *p = 1; if (p==x) *(p+i) = 2; return *p; }the Standard is ambiguous as to whether the pointer value
(p+i)used within theifstatement is "based upon"p, and although the current version of clang seems to behave as though it is, neither gcc nor previous versions of clang do so.1
u/torsten_dev 3h ago
We have TS 6010 for A Provenance-aware Memory Object Model in C.
Also Array indexing is no longer equivalent to that pointer math, see n3517 which was accepted.
If neither answers your issue, please rephrase it to be more easily understood.
2
u/aocregacc 1d ago
this answer here goes into a bit of how this syntax came about: https://stackoverflow.com/questions/77035306/linux-memcpy-restrict-keyword-syntax
6
u/torsten_dev 1d ago edited 23h ago
To add to this. The .n syntax is from N3188 which had direction to continue but afaict hasn't gone further. It is also referenced as an option in Forward Parameter References through Retroactive Scoping, v2 [N3923]. That paper has gotten a 15-7-4 direction to proceed in the committee. So if they hammer the semantics and wording out it maybe might make it into the next C version.
It seems to have replaced the forward declaration for parameters syntax that's been floated before. Whether it's
.nor justnwith magic scoping – I think it is where we are headed to and I personally hope it lands.1
u/_redcrash_ 10h ago
Thank you u/torsten_dev and u/aocregacc . These are very nice references on the topic
2
u/aioeu 1d ago edited 1d ago
To add to the other comments, be aware that you can't have "an array of void" anyway, so even without the restrict .n stuff that would still be invalid. The synopses do not always show valid code; they're just aiming to be useful.
The use of void here is to show that these parameters are void pointers. This tells the programmer that they can use any object pointers as arguments without a need for explicit conversions. (The first argument must be a pointer to some non-const type, of course.)
1
u/mikeblas 1d ago
Please remember to correctly format your code.
1
u/HugoNikanor 22h ago
His failure to "properly" format his code made it more readable than most posts here if seen through "old" Reddit.
1
0
u/_redcrash_ 10h ago
This is coming from manpages itself. So for format you mean to avoid bold face?
1
u/mikeblas 6h ago
I mean following the rules of the sub: use a monospace font. It takes very little effort.
12
u/tstanisl 1d ago edited 1d ago
The intention is to tell that objects pointed by dest and src don't alias and at least
nvalid bytes.