Re: free()'ing restrict'ed pointers



s0suk3@xxxxxxxxx wrote:

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?

Because you have lied to the compiler. The declaration of 'q' tells the
compiler that from this point forward, only 'q' or pointers derived
from it may be used to access the data, but the very next line you
violate this promise by indexing 'p'. Thus further behaviour is
undefined.

.



Relevant Pages

  • Re: forwards declarations!
    ... h, long m, int w, int l); ... compiler obviously doesn't. ... LRESULT callerFunction(HWND, long, WPARAM, LPARAM), HWND ... Not quite the same as straight forwards function pointer usage. ...
    (microsoft.public.vc.language)
  • Re: Common Problems that Compilers Dont Catch
    ... where the compiler offered no help. ... Become more familiar with pointer variables and you won't be making such ... int i = p; ... Any pointer type may be converted to an integer type. ...
    (comp.lang.c)
  • Re: Inconsistent Program Results
    ... Why do you lie to the compiler? ... you define later takes an argument, a pointer to pointer to ... Under both Windows and Linux mainalways returns an int (and ... not know about the return type of malloc(). ...
    (comp.lang.c)
  • Re: Confused with malloc
    ... malloc without prototyppe gets ... interpreted as int malland this gets you code like: ... Not every compiler uses one and the same location with exactly the ... same number of bits for int and pointer to something. ...
    (comp.lang.c)
  • Re: Newbie question
    ... mainis a function that returns an int. ... can correct that by passing a pointer to the structure to the func- ... tion and operate on the structure belonging to the caller via this ... Some of the problems the compiler can tell you about if you invoke it ...
    (comp.lang.c)