martinkunev
martinkunev

Reputation: 1405

restrict for return type and local variables

I have a good understanding of when to use restrict for function arguments. But all the articles I've found so far never mention other declarations (like function return values and local variables).

Here is one example:

extern int *alloc_foo(void);
extern int *alloc_bar(void);

int *foo = alloc_foo();
foo[i] = 42;
int *bar = alloc_bar();
f(foo[i]);

If alloc_foo() and alloc_bar() are guaranteed to return non-aliased addresses (like if there are wrappers for malloc), should I make them return restrict? Should I make foo and bar restrict?

Upvotes: 1

Views: 403

Answers (1)

R.. GitHub STOP HELPING ICE
R.. GitHub STOP HELPING ICE

Reputation: 215457

Returning restrict is not meaningful, just like returning const or volatile wouldn't be. This is because the return value of a function is purely a value expression ("rvalue"), not an expression denoting an object ("lvalue"). I don't know any way to encode the knowledge of non-aliasing in the function type without GCC attributes (whereby you could mark the function as being malloc-like) but the caller is free to store the result in a restrict-qualified pointer object and thereby give the compiler this knowledge.

Upvotes: 4

Related Questions