This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: malloc attributes and realloc


neroden@twcny.rr.com (Nathanael Nerode)  wrote on 02.01.04 in <20040103040447.GA1283@twcny.rr.com>:

> Is there a known workaround if the comparison of dead pointers may
> be optimized out of existence?

memcpy() or the moral equivalent using unions and char[].

> For instance, I might try this (if for instance memory addresses were
> the same size as longs):

You have a different problem here:

> #include <stdlib.h>
> int f(void) {
>   int* p;
>   int* q;
>   long old_addr;
>   long new_addr;
>   p = malloc(sizeof(int));
>   *p = 0;
>   old_addr = (long) p ;
>   q = realloc(p, sizeof(int));
>   new_addr = (long) q ;
>   if (old_addr == new_addr) {
>     *p = 1;

It probably isn't legal to *use* p here. (I think that is subject to a DR,  
though, see upthread.)

>     return *q;
>   }
>   return 0;
> }
>
> If that worked, I wouldn't really complain.

This might actually be code that this variant of alias analysis does kill.

> If there was another simpler workaround (like declaring p volatile or
> something) that would be even better.

Really, looking at the value of p is one thing, but actually using it to  
address memory is much, much worse.

And you obviously don't actually need it here.

> If there was simply *no* workaround, however, I would complain.  There
> has to be a reasonable way to compare the two in the case in which you
> really want to and you know what you're doing; and it shouldn't involve
> ordering the compiler not to optimize.

Sure: see above. That's the canonical way to look at things you otherwise  
couldn't.

However, it *does* tell the compiler to be careful about alias analysis  
(though probably not in a way relevant here) - this is why char* (and only  
char*) can alias with "everything", or more plainly "the compiler cannot  
know where any char* points to".

(That's probably "unsigned char" everywhere I said "char".)

MfG Kai


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]