Re: free()'ing restrict'ed pointers
- From: santosh <santosh.k83@xxxxxxxxx>
- Date: Thu, 31 Jul 2008 22:26:59 +0530
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.
.
- References:
- free()'ing restrict'ed pointers
- From: s0suk3
- Re: free()'ing restrict'ed pointers
- From: christian.bau
- Re: free()'ing restrict'ed pointers
- From: s0suk3
- free()'ing restrict'ed pointers
- Prev by Date: Re: free()'ing restrict'ed pointers
- Next by Date: Re: Procyon Library for Atmel AVR MCU - I²C problems
- Previous by thread: Re: free()'ing restrict'ed pointers
- Next by thread: Re: free()'ing restrict'ed pointers
- Index(es):
Relevant Pages
|