Re: free()'ing restrict'ed pointers



On Jul 31, 10:46 am, "christian.bau"
<christian....@xxxxxxxxxxxxxxxxxx> wrote:
On Jul 31, 2:40 pm, s0s...@xxxxxxxxx wrote:

Hi,

A object to which there is a pointer that has been declared 'restrict'
is supposed to be accessed only through that pointer. So is it safe to
call free() with a restricted pointer? Does free() try to access what
in points to in some way?

You are quoting incorrectly what "restrict" means: The data pointed to
may be accessed through the restrict pointer itself, or through a
pointer derived from that pointer. If you have a pointer "int
*restrict p" and call free (p), then the argument to free is derived
from p and is allowed to access the data as much as it likes.


Wow, seems like everybody's got a different idea about the subject.
But Ben Bacarisse said it's legal to *access* the object through
another pointer or a variable that holds the object (or array element,
etc), but not to *modify* it. Is that what you meant to? Or is it also
illegal to merely access it (through something other than the
restricted pointer or something that's not derived from it)?

Now what is wrong is the following:

int* p = malloc (100);
int* restrict q = p;

p [0] = 1;
free (q);

(the call free (q) is just as wrong as an assignment q [0] = 2 or just
reading q [0] would be).

Reading q[0] is wrong? But q is the restricted pointer; why is it
wrong to access it or modify it?

Sebastian

.



Relevant Pages

  • 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: Commenting the source code.
    ... I just prefer the first way, as when I am reading the ... is pointer to type int" in my head, even though I know the *correct* ... reading is "a is type pointer to int". ...
    (comp.lang.c)
  • Re: free()ing restricted pointers
    ... or just reading q would be). ... Because you have lied to the compiler. ... The point is that 'restrict' is not an inhibition to the compiler, ... It says that data accessible via that pointer ...
    (comp.lang.c)
  • Re: Using restricted pointers with newly allocated arrays/structures
    ... return (int) x; ... might return the same pointer. ... yet the initialization of that object to 0.0 in ... Restrict qualification becomes interesting only when there is* ...
    (comp.std.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)