What is the purpose of restrict in tmpfile_s?
Asked Answered
S

1

7

From C11 draft:

C11 (n1570), § K.3.5.1.1 The tmpfile_s function
errno_t tmpfile_s(FILE * restrict * restrict streamptr);

What is the purpose of the restrict qualifier here?

Because there is no other parameters, the compiler is able to know that streamptr is not aliased without restrict, isn't it?

Stacistacia answered 15/12, 2012 at 15:28 Comment(2)
+1 for splitting hair :PFlorin
Well, it forbids the function from ever returning a file that was created previously. What you'd expect tmpfile() to do, now cast in stone.Cockchafer
T
5

There are several global variables around that have type FILE* such as stdout and stderr for example. So the leftmost restrict clearly indicates that none of these can be returned, the returned FILE* doesn't alias with any other. The second restrict makes the same guarantee but one level higher, you are not allowed to pass something like &stderr in that function. (Well stderr is not necessarily a variable in the usual sense, but I hope you see the picture.)

Truett answered 15/12, 2012 at 16:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.