Re: free()'ing restrict'ed pointers



s0suk3@xxxxxxxxx writes:

A object to which there is a pointer that has been declared 'restrict'
is supposed to be accessed only through that pointer.

Hmmm.... A better (and I am sure this is not the last word on the
subject) is that objects that have a restrict qualified pointer to
them are only /modified/ via that pointer while it exists[1]. You can
access the value via other means, it is modifications that must be via
the pointer.

So is it safe to call free() with a restricted pointer?

Usually, yes.

Does free() try to access what in points to in some way?

I does not matter since it would be allowed to, at least by the
restrict keyword. It is possible that UB results at the exact moment
free is called in this daft code:

void *space = malloc(1);
void *restrict rp = space;
free(space); // are we modifying *rp? -- I don't know.

but that is very much an academic argument. This is fine:

void *restrict rp = malloc(1);
free(rp);

[1] This last bit plays fast a loose with the very precise definition
of the time during which the restrict restrictions apply, but it is
not a bad approximation to the truth.

--
Ben.
.



Relevant Pages

  • Re: Global restricted function pointer
    ... void; ... I _can_ declare a global restricted object pointer, ... the keyword restrict tells the compiler that two pointers ...
    (comp.lang.c)
  • Re: Inconsistent Program Results
    ... void rdinpt; ... The return type of main is int, so this should be int main, ... to increment a NULL pointer, that's undefined behavior, so anything can ... against this by making sure that restrict is not NULL, ...
    (comp.lang.c)
  • Re: restrict in plain English?
    ... it's impossible to swap a restrict pointer with another pointer, ... compiler might generate this code itself, ... The restrict qualifier only has meaning in terms of modifying an ...
    (comp.lang.c)
  • Re: universal pointer to restricted pointer
    ... register have types of the form T * restrict *, that is, a pointer to ... then using const void * would be ... a solution but restrict cannot be applied to void. ... I dont know how elegant ...
    (comp.lang.c)
  • Re: Global restricted function pointer
    ... void; ... What 'restrict' means is that all accesses to the object pointed at by the restrict-qualified pointer use, directly or indirectly, the value of that particular pointer. ... The C standard doesn't define any permitted method for accessing the memory that a function pointer points at, only a method for calling the function that the pointer points at. ...
    (comp.lang.c)